hope you are doing well today, i am working with a personal project, im using some cables to find the edges, find the center and sample the colors in hsv to then figure out the color and list it in a final image drawn on the frame, yes this is real time.
I can share the entire project if anyone is curious to see how i am doing it, because its working very interestingly, however there is a section where i feel like i may have caused some difficulties to myself:
edged_otsu = cv2.Canny(blurred, (otsu_thresh_val*0.5), otsu_thresh_val)
indices = np.where(edged_otsu == [255])
coordinates = zip(indices[0], indices[1])
listing = list(coordinates)
mylist = list(dict.fromkeys(listing))
contours, _ = cv2.findContours(edged_otsu, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
number_of_edges = len(contours) + 1
wire_colors = []
for i in range(number_of_edges):
try:
if ((mylist[i + 1][1] - mylist[i][1]) / 2) > 10: # This makes sure it is not a false edge by checking against a pixel threshold
middle = int((mylist[i][0]) + (mylist[i + 1][1] - mylist[i][1]) / 2)
middle_point = (20, mylist[i][1] + middle) # 20 is an arbitrary choice
cv2.circle(img, (middle_point[1], middle_point[0]), 2, (255, 255, 255), 2) #Draw the circle on the center
x = middle_point[0] #It may be worth it to avrg several pixels around to make the comparison better but for now...
y = middle_point[1]
hsvcolor = image_hsv[x, y]
wire_colors.append(hsvcolor)
else:
pass
except IndexError:
print("Index Error")
pass
cv2.drawContours(img, (contours), -1, (255,255,255), 2)
print(len(wire_colors), "objects were found in this image.")
return wire_colors
This is a short segment of the part that finds the edges and samples the color, i have ommited the processing as it is a bit large and it works well enough, here the issue is that i am sure that it starts the loop twice before the new frame is delivered, meaning that what should return:
wire_colors('White', 'Black', 'Blue', 'Green', 'Red')
May at times return:
wire_colors('White', 'Black', 'Blue', 'Green', 'Red', 'White', 'Black')
Which honestly baffles me, though i may have caused it with the IndexError try, this is a project id like to publicly share on my github to help others get a feeling of canny edge detection but first i need to make sure this works properly, so if anyone has any ideas i would highly appreciate you letting me know.
Added a try: except: which may have originated the issue, but otherwise the code technically breaks, i also added a pass in the index error which seemed to have no effect.