import cv2 
import time 
import os 
# ์ด๋ฏธ์ง์ ํ
์คํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์ 
def draw_text(img, text, x, y): 
    font = cv2.FONT_HERSHEY_SIMPLEX 
    font_scale = 1 
    font_thickness = 2 
    text_color=(255, 0, 0) 
    text_color_bg=(0, 0, 0) 
    text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness) 
    text_w, text_h = text_size 
    offset = 5 
    cv2.rectangle(img, (x - offset, y - offset), (x + text_w + offset, y + text_h + offset), text_color_bg, -1) 
    cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness) 
# ์น์บ  ์ฐ๊ฒฐ 
cap = cv2.VideoCapture(0) 
# ์น์บ ์์ fps ๊ฐ ํ๋ 
fps = cap.get(cv2.CAP_PROP_FPS) 
print('fps', fps) 
if fps == 0.0: 
    fps = 30.0 
time_per_frame_video = 1/fps 
last_time = time.perf_counter() 
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 
fourcc = cv2.VideoWriter_fourcc(*'mp4v') 
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 
writer = cv2.VideoWriter(BASE_DIR + '/' + 'output.mp4', fourcc, fps, (width, height)) 
while True: 
    # ์น์บ ์์ ์ด๋ฏธ์ง ์ฝ์ด์ด 
    ret,img_color = cap.read() 
    if ret == False: 
        print('์น์บ ์์ ์์์ ์ฝ์ ์ ์์ต๋๋ค.') 
        break 
    writer.write(img_color) 
    # fsp ๊ณ์ฐ 
    time_per_frame = time.perf_counter() - last_time 
    time_sleep_frame = max(0, time_per_frame_video - time_per_frame) 
    time.sleep(time_sleep_frame) 
    real_fps = 1/(time.perf_counter()-last_time) 
    last_time = time.perf_counter() 
    x = 30 
    y = 50 
    text = '%.2f fps' % real_fps 
    # ์ด๋ฏธ์ง์ (x, y)์ ํ
์คํธ ์ถ๋ ฅ 
    draw_text(img_color, text, x, y) 
    cv2.imshow("Color", img_color) 
    # ESCํค ๋๋ฅด๋ฉด ์ค์ง 
    if cv2.waitKey(1)&0xFF == 27: 
        break 
cap.release() 
writer.release() 
cv2.destroyAllWindows() 
์ฐธ๊ณ
https://webnautes.tistory.com/1662
OpenCV Python - webcam์์ ๊ฐ์ ธ์จ ์์์ mp4๋ก ์ ์ฅํ๋ ์์ 
webcam์์ ๊ฐ์ ธ์จ ์์์ mp4๋ก ์ ์ฅํ๋ OpenCV Python ์์ ์ ๋๋ค. 2022. 3. 30 ์ต์ด์์ฑ # ์ฐธ๊ณ # https://github.com/dgseten/bad-cv-tfm/blob/2ada9b71f85aa5eb75c1f4a039cb14d697ee2f69/tools/video/video-..
webnautes.tistory.com
GitHub - dgseten/bad-cv-tfm: Understanding badminton with computer vision
Understanding badminton with computer vision. Contribute to dgseten/bad-cv-tfm development by creating an account on GitHub.
github.com
Writing an mp4 video using python opencv
I want to capture video from a webcam and save it to an mp4 file using opencv. I found example code on stackoverflow (below) that works great. The only hitch is that I'm trying to save it as mp4, n...
stackoverflow.com
'๋ฅ๋ฌ๋๐ค > YOLO, Opencv ๐ข' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [YOLO]#1 YOLO๋ฅผ ํ์ฉํ ๊ฐ์ฒด ํ์ง (darknet install)&๊ตฌ๊ธ์ฝ๋ฉ (2) | 2022.11.06 | 
|---|---|
| ๋ฌด๋ฃ๋ก Yolo , Opencv ์ ๋ํด ๊ณต๋ถํ ์ ์๋ ์ฌ์ดํธ (2) | 2022.04.01 | 
| openCV๋ฅผ ์ฌ์ฉํ YOLO ๊ฐ์ฒด ๊ฐ์ง (0) | 2022.04.01 | 
| YOLO (You Only Look Once) (2) | 2022.03.23 | 
| darknet_YOLOv4 (0) | 2022.03.23 |