1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| 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) { //div(공간) 생성 const div = document.createElement('div');
//button 생성 const capture = document.createElement('button'); capture.textContent = 'Capture'; div.appendChild(capture);
//video 생성 const video = document.createElement('video'); //비디오 모양 네모네모 video.style.display = 'block'; //카메라(웹캠) 불러오기 const stream = await navigator.mediaDevices.getUserMedia({video: true});
//div 밑에 child 공간 추가 document.body.appendChild(div); //공간에 video 넣기 div.appendChild(video); //video와 웹캠 연결 video.srcObject = stream; //await -> 비동기식 처리 (thread와 관련)(async와 세트) 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);
//canvas 생성 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 삭제 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
|