Skip to content

Commit 39d08b6

Browse files
msharp9Matt Sharp
andauthored
Fixed camera bug due to second competiting program (#204)
Co-authored-by: Matt Sharp <[email protected]>
1 parent 3b279a7 commit 39d08b6

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

afy/videocaptureasync.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, src=0, width=640, height=480):
1515
self.cap = cv2.VideoCapture(self.src)
1616
if not self.cap.isOpened():
1717
raise RuntimeError("Cannot open camera")
18-
18+
1919
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
2020
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
2121
self.grabbed, self.frame = self.cap.read()
@@ -36,15 +36,15 @@ def start(self):
3636
self.thread = threading.Thread(target=self.update, args=(), daemon=True)
3737
self.thread.start()
3838

39-
# (warmup) wait for the first successfully grabbed frame
39+
# (warmup) wait for the first successfully grabbed frame
4040
warmup_start_time = time.time()
4141
while not self.grabbed:
4242
warmup_elapsed_time = (time.time() - warmup_start_time)
4343
if warmup_elapsed_time > WARMUP_TIMEOUT:
4444
raise RuntimeError(f"Failed to succesfully grab frame from the camera (timeout={WARMUP_TIMEOUT}s). Try to restart.")
4545

4646
time.sleep(0.5)
47-
47+
4848
return self
4949

5050
def update(self):
@@ -55,9 +55,14 @@ def update(self):
5555
self.frame = frame
5656

5757
def read(self):
58-
with self.read_lock:
59-
frame = self.frame.copy()
60-
grabbed = self.grabbed
58+
while True:
59+
try:
60+
with self.read_lock:
61+
frame = self.frame.copy()
62+
grabbed = self.grabbed
63+
except AttributeError:
64+
continue
65+
break
6166
return grabbed, frame
6267

6368
def stop(self):

0 commit comments

Comments
 (0)