So I am working on a project, and someone gave me some of their code that they created in python 2.7 to implement into it. The project however runs on python 3.7 and when I tried to execute it I kept getting errors related to the marker function. Would someone please be able to look at it and tell me what is missing to get the version to execute? I attached the image used to test out the function as well as the code.
Below is the error I get:
Traceback (most recent call last):
File "/home/pi/Downloads/distance_to_camera_2 (1).py", line 94, in <module>
width_array=process_component(labels_im)
File "/home/pi/Downloads/distance_to_camera_2 (1).py", line 71, in process_component
x,y,w,h = cv2.boundingRect(cnts[0])
TypeError: Expected cv::UMat for argument 'array'
This is the code:
import numpy as np
import cv2
from matplotlib import pyplot as plt
# Find distance from camera to object using Python and OpenCV
def distance_to_camera(knownWidth, focalLength, perWidth):
# compute and return the distance from the maker to the camera
return (knownWidth * focalLength) / perWidth
KNOWN_WIDTH = 8
focalLength = 545
# put your image here
img = cv2.imread("/home/pi/Downloads/many_blob.png")
cv2.imshow("img", img)
cv2.waitKey(1000)
image = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary 127
cv2.imshow("image", image)
cv2.waitKey(1000)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(image,kernel,iterations = 5)
dilate=cv2.dilate(erosion,kernel,iterations = 5)
edged = cv2.Canny(dilate, 0, 128)
cv2.imshow("edged", edged)
cv2.waitKey(1000)
connectivity=8
num_labels,labels_im = cv2.connectedComponents(edged,connectivity)
# Function only for labels display (debuging only)
def imshow_components(labels):
# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
# set bg label to black
labeled_img[label_hue==0] = 0
#labeled_img[labels==0] = 0
cv2.imshow('labeled.png', labeled_img)
cv2.waitKey(1000)
cv2.imwrite('labeled_img.png',labeled_img)
#cv2.imwrite('label_hue.png',label_hue)
def process_component(labels):
width = np.zeros(np.max(labels))
for i in range(1,np.max(labels)+1):
tmp_im= labels.copy()
tmp_im[:] = 0
tmp_im[labels==i] = 255
file="imlabel_%d.png"%(i, )
cv2.imwrite(file,tmp_im)
tmp_im = tmp_im.astype(np.uint8)
cnts = cv2.findContours(tmp_im, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# bounding box of the countour
x,y,w,h = cv2.boundingRect(cnts[0])
width[i-1] = w
tmp_im=cv2.rectangle(tmp_im,(x,y),(x+w,y+h),(255,0,0),2)
# center = center of the bounding box
center=(x+w/2,y+h/2)
cv2.circle(tmp_im, center, 3, (255,0,0), 2, 8, 0)
cv2.imshow(file, tmp_im)
cv2.waitKey(1000)
return width
width_array=process_component(labels_im)
imshow_components(labels_im)
cv2.imwrite('labels_img.png',labels_im)
for i in range(1,np.max(labels_im)+1):
w=width_array[i-1]
#marker = find_marker(image)
dist_cm = distance_to_camera(KNOWN_WIDTH, focalLength, w)
print("distance en cm = %d",dist_cm)
This is my first time posting on stack overflow so if I should post anything else for people to help me out please do tell me.
Here is the Image I have been trying to work with: https://i.stack.imgur.com/ONhUA.png