forked from Make3DPrintingProjects/Skycam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.py
91 lines (74 loc) · 2.66 KB
/
streamer.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
import time
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot, QObject, QCoreApplication
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import QApplication
from flask import Flask, render_template, Response
import cv2
from picamera import PiCamera
from picamera.array import PiRGBArray
import json
class Streamer(QObject):
flipEvt = pyqtSignal()
errEvt = pyqtSignal(object)
def __init__(self, master, cam, *args, **kwargs):
super().__init__(*args, **kwargs)
self.flipEvt.connect(self.flip)
self.cam = cam
self.cam.resolution = (640, 480)
self.cam.framerate = 32
self.rawCapture = PiRGBArray(self.cam, size=(640, 480))
self.flipped = False
def stream(self):
for fr in self.cam.capture_continuous(self.rawCapture, format="bgr", use_video_port=True):
QCoreApplication.processEvents()
time.sleep(.05)
img = fr.array
if self.flipped:
(h, w) = img.shape[:2]
center = (w / 2, h / 2)
M = cv2.getRotationMatrix2D(center, 180, 1.0)
img = cv2.warpAffine(img, M, (w, h))
s, jpg = cv2.imencode('.jpg', img)
frame = jpg.tobytes()
self.rawCapture.truncate(0)
yield b'--frame\r\nContent-Type: image/jpeg\r\nContent-Length:' + bytes(len(frame)) + b'\r\n\r\n' + frame + b'\r\n\r\n'
@pyqtSlot()
def flip(self):
print('filpping')
self.flipped = not self.flipped
print(self.flipped)
class StreamReciever(QObject):
newImage = pyqtSignal(QImage, str)
def __init__(self, master, address):
super().__init__()
self.address = address
self._isrunning = bool()
def run(self):
cap = cv2.VideoCapture(self.address)
self._isrunning = True
while cap.isOpened() and self._isrunning:
i, frame = cap.read()
if i:
image = QImage(frame.tostring(), 640, 480, QImage.Format_RGB888).rgbSwapped()
self.newImage.emit(image, self.id)
def stop(self):
self._isrunning = False
class FlaskThread(QThread):
def __init__(self, application):
super().__init__()
self.application = application
def __del__(self):
self.wait()
def run(self):
self.application.run(host='192.168.5.14', port=7777, debug=False, use_reloader=False)
def main(fapp, cam):
try:
fapp.run(host='localhost', port=7777, debug=False, use_reloader=False)
except:
cam.release()
raise
if __name__ == '__main__':
cam = cv2.VideoCapture(-1)
s = Streamer(None, cam)
main(s.fapp, cam)