11-1WebStream.py

000: import cv2
001: import bottle
002: import time
003:
004: web = bottle.Bottle()
005:
006: def __main():
007: cap = cv2.VideoCapture(0, cv2.CAP_V4L)
008: cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
009: cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
010:
011: if not cap.isOpened(): # ビデオキャプチャー可能か判断
012: print(“Not Opened Video Camera”)
013: exit()
014:
015: while True:
016: ret, img = cap.read()
017:
018: if not ret: # キャプチャー画像取得に失敗したら終了
019: print(“Video Capture Err”)
020: break
021:
022: # ここで処理を実行する
023: result, jpgImg = cv2.imencode(‘.jpg’, img=img, params=[int(cv2.IMWRITE_JPEG_QUALITY), 80]) # 0 – 100
024: yield b’–frame\r\n’ + b’Content-Type: image/jpeg\r\n\r\n’ + bytearray(jpgImg) + b’\r\n\r\n’
025: time.sleep(1 / 60)
026:
027: cap.release()
028: cv2.destroyAllWindows()
029:
030: return 0
031:
032: @web.route(‘/’)
033: def main():
034: return bottle.static_file(‘index.html’, root=’./’)
035:
036: @web.route(‘/video_recv’)
037: def video_recv():
038: bottle.response.content_type = ‘multipart/x-mixed-replace;boundary=frame’
039: return __main()
040:
041: if __name__ == ‘__main__’:
042: print(cv2.__version__)
043:
044: web.run(host=’0.0.0.0′, port=8080, reloader=True, debug=True)