I cannot run my code that I wrote on python IDE 3.11 to the python Thoonny 3.9. I downloaded the opency libraty:https://qengineering.eu/install-opencv-on-raspberry-pi.html. I also used picamera2 library but fail. How should I integrate my code to raspberry pi 4 python Thoonny 3.9?
Error:
[ WARN:[email protected]] global cap_gstreamer.cpp:2784 handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory.
[ WARN:[email protected]] global cap_gstreamer.cpp:1679 open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:[email protected]] global cap_gstreamer.cpp:1164 isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
failed to grab frame
[ WARN:[email protected]] global loadsave.cpp:244 findDecoder imread_('analog_gauge_30.png'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "<string>", line 152, in <module>
File "<string>", line 41, in take_measure
AttributeError: 'NoneType' object has no attribute 'shape'
import cv2
import numpy as np
import time
def avg_circles(circles, b):
avg_x = 0
avg_y = 0
avg_r = 0
for i in range(b):
avg_x = avg_x + circles[0][i][0]
avg_y = avg_y + circles[0][i][1]
avg_r = avg_r + circles[0][i][2]
avg_x = int(avg_x / (b))
avg_y = int(avg_y / (b))
avg_r = int(avg_r / (b))
return avg_x, avg_y, avg_r
def dist_2_pts(x1, y1, x2, y2):
return np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
def take_measure(threshold_img, threshold_ln, minLineLength, maxLineGap, diff1LowerBound, diff1UpperBound, diff2LowerBound, diff2UpperBound):
cam = cv2.VideoCapture(0)
cv2.namedWindow("test")
img_counter = 30
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k % 256 == 27:
print("Escape hit, closing...")
break
elif k % 256 == 32:
img_name = "analog_gauge_{}.png".format(img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
break
img = cv2.imread("analog_gauge_30.png")
height, width = img.shape[:2]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20)
if circles is not None:
a, b, c = circles.shape
x, y, r = avg_circles(circles, b)
cv2.circle(img, (x, y), r, (0, 255, 0), 3, cv2.LINE_AA)
cv2.circle(img, (x, y), 2, (0, 255, 0), 3, cv2.LINE_AA)
min_angle = 0
max_angle = 360
min_value = 0
max_value = 16
separation = 10
interval = int(360 / separation)
p1 = np.zeros((interval, 2))
p2 = np.zeros((interval, 2))
p_text = np.zeros((interval, 2))
for i in range(0, interval):
for j in range(0, 2):
if (j % 2 == 0):
p1[i][j] = x + 0.9 * r * np.cos(separation * i * 3.14 / 180)
else:
p1[i][j] = y + 0.9 * r * np.sin(separation * i * 3.14 / 180)
text_offset_x = 10
text_offset_y = 5
for i in range(0, interval):
for j in range(0, 2):
if (j % 2 == 0):
p2[i][j] = x + r * np.cos(separation * i * 3.14 / 180)
p_text[i][j] = x - text_offset_x + 1.2 * r * np.cos((separation) * (i + 9) * 3.14 / 180)
else:
p2[i][j] = y + r * np.sin(separation * i * 3.14 / 180)
p_text[i][j] = y + text_offset_y + 1.2 * r * np.sin((separation) * (i + 9) * 3.14 / 180)
for i in range(0, interval):
cv2.line(img, (int(p1[i][0]), int(p1[i][1])), (int(p2[i][0]), int(p2[i][1])), (0, 255, 0), 2)
cv2.putText(img, '%s' % (int(i * separation)), (int(p_text[i][0]), int(p_text[i][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1, cv2.LINE_AA)
cv2.putText(img, "Gauge OK!", (50, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2, cv2.LINE_AA)
gray3 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
maxValue = 255
th, dst2 = cv2.threshold(gray3, threshold_img, maxValue, cv2.THRESH_BINARY_INV);
cv2.imshow('test3', dst2)
dst2 = cv2.medianBlur(dst2, 5)
dst2 = cv2.Canny(dst2, 50, 150)
dst2 = cv2.GaussianBlur(dst2, (5, 5), 0)
in_loop = 0
lines = cv2.HoughLinesP(image=dst2, rho=3, theta=np.pi / 180, threshold=threshold_ln, minLineLength=minLineLength, maxLineGap=maxLineGap)
final_line_list = []
for i in range(0, len(lines)):
for x1, y1, x2, y2 in lines[i]:
diff1 = dist_2_pts(x, y, x1, y1)
diff2 = dist_2_pts(x, y, x2, y2)
if (diff1 > diff2):
temp = diff1
diff1 = diff2
diff2 = temp
if (((diff1 < diff1UpperBound * r) and (diff1 > diff1LowerBound * r) and (diff2 < diff2UpperBound * r)) and (diff2 > diff2LowerBound * r)):
line_length = dist_2_pts(x1, y1, x2, y2)
final_line_list.append([x1, y1, x2, y2])
in_loop = 1
if (in_loop == 1):
x1 = final_line_list[0][0]
y1 = final_line_list[0][1]
x2 = final_line_list[0][2]
y2 = final_line_list[0][3]
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 255), 2)
dist_pt_0 = dist_2_pts(x, y, x1, y1)
dist_pt_1 = dist_2_pts(x, y, x2, y2)
if (dist_pt_0 > dist_pt_1):
x_angle = x1 - x
y_angle = y - y1
else:
x_angle = x2 - x
y_angle = y - y2
res = np.arctan(np.divide(float(y_angle), float(x_angle)))
res = np.rad2deg(res)
if x_angle > 0 and y_angle > 0:
final_angle = 270 - res
if x_angle < 0 and y_angle > 0:
final_angle = 90 - res
if x_angle < 0 and y_angle < 0:
final_angle = 90 - res
if x_angle > 0 and y_angle < 0:
final_angle = 270 - res
old_min = float(min_angle)
old_max = float(max_angle)
new_min = float(min_value)
new_max = float(max_value)
old_value = final_angle
old_range = (old_max - old_min)
new_range = (new_max - new_min)
new_value = ((((old_value - old_min) * new_range) / old_range) + new_min) - 1.4
cv2.putText(img, "Indicator OK!", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2, cv2.LINE_AA)
cv2.putText(img, str(new_value), (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2, cv2.LINE_AA)
print("Res:", res)
print("Final Angle: ", final_angle)
print("New value", new_value)
else:
cv2.putText(img, "Can't find the indicator!", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2, cv2.LINE_AA)
else:
cv2.putText(img, "Can't see the gauge!", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2, cv2.LINE_AA)
return img, img
while True:
threshold_img = 120
threshold_ln = 150
minLineLength = 40
maxLineGap = 8
diff1LowerBound = 0.15
diff1UpperBound = 0.25
diff2LowerBound = 0.5
diff2UpperBound = 1.0
img, img2 = take_measure(threshold_img, threshold_ln, minLineLength, maxLineGap, diff1LowerBound, diff1UpperBound, diff2LowerBound, diff2UpperBound)
cv2.imshow('Analog Gauge RESULT', img2)
if ord('q') == cv2.waitKey(1):
break