1

I am developing a GUI with PyQt5 and I am stuck.

Because my program is running on a RaspberryPi4 I have limited processing power. I am getting video input from my webcam and want to perform face_recognition operations on this input. Due to the limited processing power i need to ignore a lot of input frames and just use every n-th frame to perform face recognition with to speed up the process.

I tried to program a delay similar to this thread: Call function every x seconds (Python) but it didn't work. Is there a possibility to directly refer to a frame?

This is the function where I am reading from the webcam:


def run(self):
            checker=0
            process_this_frame = 0
            # capture from web cam
            cap = cv2.VideoCapture(0)
            cap.set(cv2.CAP_PROP_FRAME_WIDTH,640);
            cap.set(cv2.CAP_PROP_FRAME_HEIGHT,300);
            while True:
                ret, cv_img = cap.read()
                if ret:
                    img = cv2.resize(cv_img, (0, 0), fx=0.25, fy=0.25)
                    process_this_frame = process_this_frame+2
                    print('process_this_frame: ' , process_this_frame)
                    if process_this_frame % 20 == 0:
                       
                        predictions = predict(img, model_path="trained_knn_model.clf")
                        print('showing predicted face')
                        cv_img = show_prediction_labels_on_image(cv_img, predictions)
                        checker=1
                        self.change_pixmap_signal.emit(cv_img)
                        
                    else:
                        checker=0
                        self.change_pixmap_signal.emit(cv_img)

Specifically I am looking for a suitable if condition to execute the predict function only on every n-th frame and when I am not doing the predict on the cv_img I want to display just the frame in my else case. I tried with multiple modulo operators but did not find a suitable solution.

How can I do that? It would be cool to refer to a number of frames instead of using a time delay so I can try to find the best solution.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
julius
  • 11
  • 2
  • Show output of code please. Probably best to add 1 rather than 2 each loop just for ease of understanding, but modulo arithmetic should work for this task – DerekG Apr 22 '21 at 14:19
  • Thanks for replying @DerekG after your comment i tried multiple variants with the modulo operator and it worked. I just was not sure if i was even on the right track. – julius May 11 '21 at 11:05

0 Answers0