Yolov5 customize webcam

Raspberry Pi, Jeston Nano


henrywu
Posts: 202
Joined: Sun Apr 17, 2022 4:57 pm

Yolov5 customize webcam

Post by henrywu »

Code: Select all

diff --git a/utils/dataloaders.py b/utils/dataloaders.py
index 23ee2d5..5d140ee 100755
--- a/utils/dataloaders.py
+++ b/utils/dataloaders.py
@@ -321,7 +321,12 @@ class LoadStreams:
                 import pafy
                 s = pafy.new(s).getbest(preftype="mp4").url  # YouTube URL
             s = eval(s) if s.isnumeric() else s  # i.e. s = '0' local webcam
-            cap = cv2.VideoCapture(s)
+            cap = cv2.VideoCapture(s, cv2.CAP_V4L2)
+            cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
+            cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
+            cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
+            cap.set(cv2.CAP_PROP_FPS, 60)
+
             assert cap.isOpened(), f'{st}Failed to open {s}'
             w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
             h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))