Study/class note
딥러닝 / 실시간 웹캠 오브젝 디텍션
chanzae
2022. 5. 9. 13:55
1. 동영상 실시간으로 확인
import matplotlib.pyplot as plt
import cv2
import numpy as np
from IPython.display import display, Image
import ipywidgets as widgets
import threading
# Stop button
# ================
stopButton = widgets.ToggleButton(
value=False,
description='Stop',
disabled=False,
button_style='danger', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='square' # (FontAwesome names without the `fa-` prefix)
)
# Display function
# ================
def view(button):
cap = cv2.VideoCapture(0)
display_handle=display(None, display_id=True)
i = 0
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1) # if your camera reverses your image
_, frame = cv2.imencode('.jpeg', frame)
display_handle.update(Image(data=frame.tobytes()))
if stopButton.value==True:
cap.release()
display_handle.update(None)
# Run
# ================
display(stopButton)
thread = threading.Thread(target=view, args=(stopButton,))
thread.start()
========================주석
# Stop button
# ================
stopButton = widgets.ToggleButton(
value=False,
description='Stop',
disabled=False,
button_style='danger', # 'success', 'info', 'warning', 'danger' or ''
tooltip='Description',
icon='square' # (FontAwesome names without the `fa-` prefix)
)
# Display function
# ================
def view(button):
cap = cv2.VideoCapture(0) # 실시간으로 PC와 연결된 카메라가 보내주는 영상을 실행해서 cap에 담음
display_handle=display(None, display_id=True) # 주피터에서 실행할 변수 생성
i = 0
while True:
_, frame = cap.read() # 영상을 읽어서 frame변수에 넣음
frame = cv2.flip(frame, 1) # if your camera reverses your image #좌우 반전
_, frame = cv2.imencode('.jpeg', frame) # 3차원이 1차원으로 바뀜(flatten)
display_handle.update(Image(data=frame.tobytes()))
# 주피터 노트북에서 실시간으로 카메라 영상을 띄워줌
if stopButton.value==True:
cap.release()
display_handle.update(None) # display종료
# Run
# ================
display(stopButton) # stop버튼 표시
thread = threading.Thread(target=view, args=(stopButton,)) # 위에서 만든 view함수를 실행, 입력값으로 stop을 받음
thread.start() # 실행
반응형