import numpy as np
import cv2
import math
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while True:
ret, img = cap.read()
if img is None:
break
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 120)
lines = cv2.HoughLinesP(edges, 1, math.pi/1, 20, None, 2, 480);
dot1 = (lines[0][0][0],lines[0][0][1])
dot2 = (lines[0][0][2],lines[0][0][3])
cv2.line(img, dot1, dot2, (255,0,0), 3)
cv2.imshow("output", img)
length = lines[0][0][1] - lines[0][0][3]
print (length)
key = cv2.waitKey(10)
if key == 27:
break
cv2.destroyAllWindows()
cv2.VideoCapture(0).release()
4 comments:
Hi, thanks for the code.But I'm getting an error, please help me with it.
Traceback (most recent call last):
File "C:/Users/Hari/Downloads/New folder/liquid.py",
line 17, in
dot1 = (lines[0][0][0],lines[0][0][1])
TypeError: 'NoneType' object is not subscriptable
Thats bcoz it didnt detect any lines in the image/frame
This code fixes the crashes:
import cv2
import math
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
while True:
ret, img = cap.read()
if img is None:
break
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 100, 120)
lines = cv2.HoughLinesP(edges, 1, math.pi / 1, 20, None, 2, 480)
if lines is None:
print('No Hough Lines found in image.')
break
else:
dot1 = (lines[0][0][0], lines[0][0][1])
dot2 = (lines[0][0][2], lines[0][0][3])
cv2.line(img, dot1, dot2, (255, 0, 0), 3)
cv2.imshow("output", img)
length = lines[0][0][1] - lines[0][0][3]
print(length)
key = cv2.waitKey(10)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
Post a Comment