6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PCをインスタント・スパイカムにする

Posted at

PCに20行以内でスパイカムを作成する。(所要時間10分)

1.あらかじめusbで動作するpython(python,bottle,cv2) を用意する。
2.wifiを確認(アドレス 例は、192168.1.10)する。
3.下記のプログラムをusbに入れる。
4.実行する

spycam.py
from bottle import *
import cv2
class VideoCamera(object):
    def __init__(self):
        self.video = cv2.VideoCapture(0)
    def __del__(self):
        self.video.release()    
    def get_frame(self):
        return cv2.imencode('.jpg', self.video.read()[1])[1].tobytes()
def gen(camera):
    while True:
        yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + camera.get_frame() + b'\r\n\r\n')
@route('/video_feed')
def video_feed():
    response.content_type = 'multipart/x-mixed-replace; boundary=frame'
    return gen(VideoCamera())
@route('/')
def index(): 
    return '<div align="center"><h1>Video Streaming</h1><img src="/video_feed"></div>'
run(host='192.168.1.10', port=911)

解説


return cv2.imencode('.jpg', self.video.read()[1])[1].tobytes()

tupleで返される値を一行に書いた。
一般的な御作法で書くと・・・

success, image = self.video.read()       
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()

yieldを用いてmotion jpegを作成

yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + camera.get_frame() + b'\r\n\r\n')

「Stupid Phone」から http://192.168.1.10:911 アクセスする。

cap1.PNG

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?