반응형
츄르사려고 코딩하는집사입니다.
1. colab.research.google.com 사이트에 왼쪽 Sider bar <> 기호를 눌러 Camera Capture의 오른쪽 화살표 표시를 누른다.
2. 카메라 캡처를 하려면 총 4개의 코드를 돌려야 한다.
1) 카메라 캡처에 필요한 라이브러리 import 및 카메라 캡처 함수
from IPython.display import display, Javascript
from google.colab.output import eval_js
from base64 import b64decode
def take_photo(filename='photo.jpg', quality=0.8):
js = Javascript('''
async function takePhoto(quality) {
const div = document.createElement('div');
const capture = document.createElement('button');
capture.textContent = 'Capture';
div.appendChild(capture);
const video = document.createElement('video');
video.style.display = 'block';
const stream = await navigator.mediaDevices.getUserMedia({video: true});
document.body.appendChild(div);
div.appendChild(video);
video.srcObject = stream;
await video.play();
// Resize the output to fit the video element.
google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);
// Wait for Capture to be clicked.
await new Promise((resolve) => capture.onclick = resolve);
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
stream.getVideoTracks()[0].stop();
div.remove();
return canvas.toDataURL('image/jpeg', quality);
}
''')
display(js)
data = eval_js('takePhoto({})'.format(quality))
binary = b64decode(data.split(',')[1])
with open(filename, 'wb') as f:
f.write(binary)
return filename
2) 카메라 캡처 함수를 실행시켜 카메라 캡처 기능과 캡처한 사진 출력
from IPython.display import Image
try:
filename = take_photo()
print('Saved to {}'.format(filename))
# Show the image which was just taken.
display(Image(filename))
except Exception as err:
# Errors will be thrown if the user does not have a webcam or if they do not
# grant the page permission to access it.
print(str(err))
아래의 사진처럼 캡처한 결과가 출력된다.
3) openCV 라이브러리를 이용한 imgshow
from google.colab.patches import cv2_imshow
!curl -o logo.png https://colab.research.google.com/img/colab_favicon_256px.png
import cv2
img = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)
cv2_imshow(img)
반응형
'AI' 카테고리의 다른 글
코랩(Colab) Mounting Google Drive in your VM (0) | 2021.07.02 |
---|---|
Visualization: Linked Brushing in Altair (0) | 2021.07.02 |
최근댓글