I'm working in a project with OCR using a webcam. I defined a capture() function for save the frame that contains minimum 20 contours with areas greater than 60 pixels in lapses of 3 seconds. I need that the main while cycle works all the time. So I'm using a thread to call capture() function. When I run the code the Python Shell returned an error: NameError: global name frame, ln2 are not defined. The 13th commented line solves the error for the variable frame. Does it means that I have to replicate all the code that is inside the while cycle?
I'm using python 2.7 on Windows 7.
Here is the code:
import cv2
import time
import threading
cap = cv2.VideoCapture(0)
def capture():
global frame, ln2
if ln2 > 20:
cv2.imwrite("frame.jpg", frame)
time.sleep(3)
#ret, frame = cap.read() #it solves the error for variable 'frame'
child_t = threading.Thread(target = capture)
child_t.setDaemon(True)
child_t.start()
while(1):
a = []
ret, frame = cap.read()
img1 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
_, img2 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY)
(_, contornos, _) = cv2.findContours(img2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ln = len(contornos)
for i in range (0, ln):
cn = contornos[i]
x, y, w, h = cv2.boundingRect(cn)
area = 2*(w+h)
if area > 60 and area < 1000:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0, 255, 0), 2)
a.append(area)
ln2 = len(a)
print ln2
#here I want to call capture() function
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) & 0xFF == ord('x'):
break
child_t.join()
cap.release()
cv2.destroyAllWindows()
Here you go. Note that I'm using
threading.Timerinstead ofthreading.Threadfollowed by atime.sleep.Also, You said you need to save the frame that contains minimum 20 contours with areas greater than 60 pixels, but the related
ifstatement in your code doesn't do that. So I've added that as well.The message NameError: global name frame, ln2 are not defined is because the thread is started even before
frameis being read. Same applies to the variableln2as well. This is also fixed in the code below. Basically I used the flagwriteToFileto overcome this issue.