2016-10-15 1 views
0

화면 스트리밍을 위해 here 코드를 수정하려고합니다. 위의 튜토리얼에서는 스크린 샷을 찍으려고하는 반면 디스크에서 이미지를 읽는 것이 었습니다. 이 오류가 나타납니다.파이썬 화면 캡처 오류

assert isinstance(data, bytes), 'applications must write bytes' AssertionError: applications must write bytes

작동하려면 어떻게해야합니까?

내가 지금까지 한 일이다 -

<br>index.html<br> 
<html> 
    <head> 
    <title>Video Streaming Demonstration</title> 
    </head> 
    <body> 
    <h1>Video Streaming Demonstration</h1> 
    <img src="{{ url_for('video_feed') }}"> 
    </body> 
</html> 


app.py

#!/usr/bin/env python 
from flask import Flask, render_template, Response 
import time 
# emulated camera 
from camera import Camera 

# Raspberry Pi camera module (requires picamera package) 
# from camera_pi import Camera 

app = Flask(__name__) 


@app.route('/') 
def index(): 
    """Video streaming home page.""" 
    return render_template('index.html') 


def gen(camera): 
    """Video streaming generator function.""" 
    while True: 
     time.sleep(0.1) 
     frame = camera.get_frame() 
     yield (frame) 


@app.route('/video_feed') 
def video_feed(): 
    """Video streaming route. Put this in the src attribute of an img tag.""" 
    return Response(gen(Camera()), 
        mimetype='multipart/x-mixed-replace; boundary=frame') 


if __name__ == '__main__': 
    app.run(host='0.0.0.0', debug=True, threaded=True) 


camera.py

from time import time 

from PIL import Image 
from PIL import ImageGrab 
import sys 

if sys.platform == "win32": 
    grabber = Image.core.grabscreen 

class Camera(object): 

    def __init__(self): 
     #self.frames = [open('shot0' + str(f) + '.png', 'rb').read() for f in range(1,61)] 
     self.frames = [ImageGrab.grab() for f in range(1,61)] 

    def get_frame(self): 
     return self.frames[int(time()) % 3] 

전체 오류 : Link

+0

는 전체 스택 추적을 게시 할 수 오류의? – xli

+0

@xli가 스택 추적을 추가했습니다. – user6945506

답변

0

응답 페이로드는 일련의 바이트 여야합니다. 이 예제에서 반환 된 이미지는 JPEG이고 bytes 개체입니다.

그러나 ImageGrab.grab()에 의해 반환 된 이미지는 바이트 대신 일부 PIL 이미지 클래스입니다. 그래서, bytes로 JPEG로 이미지를 저장하려고 :

import io 

테이크 스크린 샷 만 세대의 모든 반복에 대해 :

class Camera(object): 
    def get_frame(self): 
     frame = ImageGrab.grab() 
     img_bytes = io.BytesIO() 
     frame.save(img_bytes, format='JPEG') 
     return img_bytes.getvalue() 

세대 기능 :

def gen(camera): 
    while True: 
     time.sleep(0.1) 
     frame = camera.get_frame() 
     yield (b'--frame\r\n' 
       b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') 
+0

여전히 출력이 표시되지 않지만 오류는 없습니다. – user6945506

+0

'yield (b'- frame \ r \ n ' b'Content-Type : image/jpeg \ r \ n \ r \ n'콘텐츠 유형이있는 원래 예제의 yield 문을 사용해야합니다. + 프레임 + b '\ r \ n')' – xli

+0

업데이트 된 답변보기. – xli