๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋”ฅ๋Ÿฌ๋‹๐Ÿค–/YOLO, Opencv ๐Ÿข

webcam์—์„œ ๊ฐ€์ ธ์˜จ ์˜์ƒ์„ mp4๋กœ ์ €์žฅํ•˜๋Š” OpenCV Python

by @ENFJ 2022. 4. 1.

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

https://github.com/dgseten/bad-cv-tfm/blob/2ada9b71f85aa5eb75c1f4a039cb14d697ee2f69/tools/video/video-player-wait-fps.py

 

 

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

https://stackoverflow.com/questions/30509573/writing-an-mp4-video-using-python-opencv/41666642#41666642โ€‹

 

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