I'm attempting to find vertical lines in a video. Python 2.7 and OpenCV 3.
I am using background subtraction and then applying the Canny Edge Detection filter.
I've been able to apply the HoughLinesP method to a single image but need to expand this to a video.
I'm receiving this error when running a basic set of code (which I believe corresponds to the line "a,b,c = hlines.shape" below):
Video file not grabbed
Traceback (most recent call last):
File "test2.py", line 60, in <module>
cv2.line(camera, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)
TypeError: img is not a numpy array, neither a scalar
Now, a couple of of weird things are going on, first 'img' isn't a variable name in this script (although it is used in other scripts that I've written in the past and have called HoughLinesP on... although that probably doesn't matter here). The other oddity is that this same code works just fine on a .PNG image taken from this video file.
I am able to open the video file just fine and apply the filters mentioned above.
Now, interestingly, for some reason... the "if not args.get("video", False):" case is being entered, too. Even though I can "reach" that video file via the terminal just fine.
I can also output (print(hlines.shape)) just fine...
Here's the code. Commenting out the lines of the 'for loop' allow this to run just fine.
import cv2
import numpy as np
import imutils
import argparse
np.set_printoptions(threshold=np.inf) #to print entire array, no truncation
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help = "/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4")
args = vars(ap.parse_args())
LOWER_BOUND = 55 #cv2.threshold()
UPPER_BOUND = 255 #cv2.threshold()
CANNY_LOWER_BOUND = 10 #cv2.Canny()
CANNY_UPPER_BOUND = 250 #cv2.Canny()
MIN_LINE_LENGTH = 2 #HoughLinesP()
MAX_LINE_GAP = 100 #HoughLinesP()
HOUGH_THETA = np.pi/180 #HoughLinesP() angle resolution of the accumulator, radians
HOUGH_THRESHOLD = 25 #HoughLinesP()
HOUGH_RHO = 1 #HoughLinesP() rho, Distance resolution of the accumulator, pixels
#bkgnd = cv2.bgsegm.createBackgroundSubtractorMOG()
camera = cv2.VideoCapture('/home/odroid/Desktop/python_scripts/test/test_images/Edited_Foam_Dispense_Short.mp4')
# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
camera = cv2.VideoCapture(0)
print("Video file not grabbed")
# otherwise, grab a reference to the video file
else:
camera = cv2.VideoCapture(args["video"])
while(True):
(grabbed, frame) = camera.read()
# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
if args.get("video") and not grabbed:
break
# resize the frame, blur it, and convert it to the HSV
# color space
frame = imutils.resize(frame, width=600)
canny_threshold = cv2.Canny(frame, CANNY_LOWER_BOUND, CANNY_UPPER_BOUND)
hlines = cv2.HoughLinesP(canny_threshold, HOUGH_RHO, HOUGH_THETA, MIN_LINE_LENGTH, MAX_LINE_GAP)
a,b,c = hlines.shape
for k in range(a):
#pretty sure the issue is somewhere here
cv2.line(camera, (hlines[k][0][0], hlines[k][0][1]), (hlines[k][0][2], hlines[k][0][3]), (0,255,0), 3, cv2.LINE_AA)
cv2.imshow("Frame", frame)
cv2.imshow('image', canny_threshold)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
Any suggestions would be appreciated.