-2

Here's my code with opencv-python. I'm trying to write a python script using mediapipe to detect a person in a video. It doesn't show any error but the video capture/playback window disappears as soon as it opens.

import cv2
import numpy as np
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

cv2.namedWindow('Annotated Frame', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('Annotated Frame', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)



# Initialize object detector
base_options = python.BaseOptions(model_asset_path='efficientdet.tflite')
options = vision.ObjectDetectorOptions(base_options=base_options, score_threshold=0.5)
detector = vision.ObjectDetector.create_from_options(options)

# Open video capture
video_path = "path_to_your_video.mp4"
cap = cv2.VideoCapture("C:\\Users\\ssart\\OneDrive\\Desktop\\NFX Tasks\\Copy of market.mp4")

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # Convert frame to MediaPipe Image format
    mp_frame = mp.Image(frame)

    # Detect objects in the frame
    detection_result = detector.detect(mp_frame)


    # Visualize and display the frame with detections
    def visualize(image, detection_result):
        for detection in detection_result.detections:
            bbox = detection.location_data.relative_bounding_box
            ih, iw, _ = image.shape
            x = int(bbox.xmin * iw)
            y = int(bbox.ymin * ih)
            w = int(bbox.width * iw)
            h = int(bbox.height * ih)

            category = detection.category[0].label
            score = detection.category[0].score
            label = f"{category}: {score:.2f} \u00b0C"

            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(image, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        return image


    while cap.isOpened():
        try:
            ret, frame = cap.read()
            if not ret:
                break

            mp_frame = mp.Image(frame)
            detection_result = detector.detect(mp_frame)

            annotated_frame = visualize(frame, detection_result)

            cv2.imshow('Annotated Frame', annotated_frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        except Exception as e:
            print("An error occurred:", e)
            break


# Release video capture and close windows
cap.release()
cv2.destroyAllWindows()

I expected the code to run the whole video while detecting the person and the objects that are being shown in the video. How can solve this issue?

Musabbir Arrafi
  • 744
  • 4
  • 18
  • https://stackoverflow.com/questions/51143458/difference-in-output-with-waitkey0-and-waitkey1 – beaker Aug 27 '23 at 19:53

0 Answers0