From 25bb82eec612803f600a2eecd3336ffa23c2cce7 Mon Sep 17 00:00:00 2001 From: Gaurav Date: Sat, 23 Apr 2022 13:02:48 +0530 Subject: [PATCH 1/2] Multithreading for faster camera frame access --- .../human_detection/web-template/hal.py | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/exercises/static/exercises/human_detection/web-template/hal.py b/exercises/static/exercises/human_detection/web-template/hal.py index c102343ba..96d67ef49 100644 --- a/exercises/static/exercises/human_detection/web-template/hal.py +++ b/exercises/static/exercises/human_detection/web-template/hal.py @@ -1,5 +1,33 @@ import cv2 import os +import threading +from threading import * + + +class WebcamStream: + def __init__(self,id=0): + self.id = id + self.cap = cv2.VideoCapture(id) + self.start_flag = False + + + def start(self): + self.t = Thread(target=self.update) + self.t.daemon = True + self.t.start() + self.start_flag = True + + + def update(self): + while True: + self.flag,self.frame = self.cap.read() + if self.flag is False: + print("\nError getting Frame") + break + self.cap.release() + + def read(self): + return self.frame class HAL: @@ -8,7 +36,7 @@ def __init__(self): # Saving the current path for later use. # The current path is somehow required everytime for accessing files, when the exercise is running in the docker container. self.current_path = os.path.dirname(os.path.abspath(__file__)) - self.cameraCapture = cv2.VideoCapture(0) + self.cameraCapture = WebcamStream(id=0) self.benchmark_vid_capture = cv2.VideoCapture(self.current_path + "/benchmarking/test_vid/video.avi") self.uploaded_vid_capture = cv2.VideoCapture(self.current_path + "/uploaded_video.mp4") #path to the ground truth detections directory @@ -16,7 +44,9 @@ def __init__(self): self.frame_number = 0 def getImage(self): - success, frame = self.cameraCapture.read() + if(self.cameraCapture.start_flag==False): + self.cameraCapture.start() + frame = self.cameraCapture.read() return frame def getBenchmarkVid(self): From 351e421c9bf654868eb357f50b6bd95339f32c8b Mon Sep 17 00:00:00 2001 From: Gaurav Date: Thu, 28 Apr 2022 14:06:54 +0530 Subject: [PATCH 2/2] Added Comments --- .../static/exercises/human_detection/web-template/hal.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/exercises/static/exercises/human_detection/web-template/hal.py b/exercises/static/exercises/human_detection/web-template/hal.py index 96d67ef49..f696802cc 100644 --- a/exercises/static/exercises/human_detection/web-template/hal.py +++ b/exercises/static/exercises/human_detection/web-template/hal.py @@ -3,15 +3,17 @@ import threading from threading import * - +#Multi-threading frame access class class WebcamStream: def __init__(self,id=0): self.id = id self.cap = cv2.VideoCapture(id) + #flag to check if the thread is started self.start_flag = False def start(self): + #Seperate thread for update function self.t = Thread(target=self.update) self.t.daemon = True self.t.start() @@ -19,6 +21,7 @@ def start(self): def update(self): + #Access frames while True: self.flag,self.frame = self.cap.read() if self.flag is False: @@ -27,6 +30,7 @@ def update(self): self.cap.release() def read(self): + #returning the frames return self.frame @@ -36,6 +40,7 @@ def __init__(self): # Saving the current path for later use. # The current path is somehow required everytime for accessing files, when the exercise is running in the docker container. self.current_path = os.path.dirname(os.path.abspath(__file__)) + #Custom class object self.cameraCapture = WebcamStream(id=0) self.benchmark_vid_capture = cv2.VideoCapture(self.current_path + "/benchmarking/test_vid/video.avi") self.uploaded_vid_capture = cv2.VideoCapture(self.current_path + "/uploaded_video.mp4") @@ -44,6 +49,7 @@ def __init__(self): self.frame_number = 0 def getImage(self): + #Starting the thread if not started if(self.cameraCapture.start_flag==False): self.cameraCapture.start() frame = self.cameraCapture.read()