Skip to content

Instantly share code, notes, and snippets.

@YuzuRyo61
Last active August 6, 2019 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuzuRyo61/2197edf86f597fb428d42121cec3c623 to your computer and use it in GitHub Desktop.
Save YuzuRyo61/2197edf86f597fb428d42121cec3c623 to your computer and use it in GitHub Desktop.
ウェブカメラの画像をキャプチャする簡易ウェブサーバー(Bottle.py、OpenCV使用)
#!/usr/bin/env python3
import cv2
import os
from bottle import route, run, static_file, abort
@route('/')
def capture():
cap = cv2.VideoCapture(0)
# 解像度は適宜変更してください
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1600)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1200)
if cap.isOpened() is False:
abort(500, 'Camera cannot enabled.')
return
ret, frame = cap.read()
if ret == False:
abort(500, 'Camera could not capture an image.')
return
cv2.imwrite(os.path.join(os.getcwd(), '.captured.jpg'), frame)
cap.release()
return static_file('.captured.jpg', root=os.getcwd())
if __name__ == "__main__":
run(host="0.0.0.0", port=4678)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment