Search code examples
pythonopencvobject-detectionvideo-processingimageai

Unable to draw lines using OpenCV


I have created a per_frame function that is fed into ImageAI's detector. I want to draw a line between centroid that meet the distance criteria (if distance >= find_dist_ratio(x1, y1)). Lines should be drawn between the centroids of all objects that meet the criteria, I tried changing it and finally got it without errors but the line does not show up in the output video. Thanks for the help!

def dist_func(counting, output_objects_array,output_objects_count):
     a =[]
     ret, frame = camera.read()
     for d in output_objects_array:
         x1 = d['box_points'][0]
         y1 = d['box_points'][1]
         x2 = d['box_points'][2]
         y2 = d['box_points'][3]
         centroid = (int((x1 + x2) / 2), int((y1 + y2) / 2))
         a.append(centroid)
     for i in range(len(a)):
         for j in range(i+1, len(a)):
            distance = euc_dist(a[i],a[j])
            if distance >= find_dist_ratio(x1, y1):
                print('close enough')
                x, y = a[i]
                X, Y = a[j]
                cv2.line(frame, (x, y), (X, Y), (255, 0, 0), 5)

Solution

  • It may sound silly, but in your piece of code I can't see if you are really showing the frame. And if the x and y variables are correct (from lower/upper case)

    See this example from the docs:

    # Create a black image
    img = np.zeros((512,512,3), np.uint8)
    # Draw a diagonal blue line with thickness of 5 px
    cv.line(img,(0,0),(511,511),(255,0,0),5)
    

    For displaying the line drawn here you should also place (after drawing)

    cv2.imshow("Line draw", img)
    

    Drawing functions in the docs