-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImgServer.py
executable file
·146 lines (124 loc) · 4.19 KB
/
ImgServer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
'''
Original Author: Igor Maculan - [email protected]
A Simple mjpg stream http server
Added commnand server for UMSS project DroneSimon
'''
import threading
import cv2
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import StringIO
import time
import VisualFilters
import Reconocedor_Fuego_Humo
from PIL import Image
capture=None
mode = 0
class CommandHandler(BaseHTTPRequestHandler):
def do_GET(self):
global mode
print self.path
cmd = self.path
self.send_response(200)
if cmd == "/?cmd=0" :
mode = VisualFilters.NADA
if cmd == "/?cmd=1" :
mode = VisualFilters.RESALTAR_CUERPOS
elif cmd == "/?cmd=2" :
mode = VisualFilters.RESALTAR_HUMO
elif cmd == "/?cmd=3" :
mode = VisualFilters.RESALTAR_FUEGO
elif cmd == "/?cmd=4" :
mode = VisualFilters.RESALTAR_BORDES
elif cmd == "/?cmd=5" :
mode = VisualFilters.RESALTAR_LINEAS_RECTAS
elif cmd == "/?cmd=6" :
mode = VisualFilters.RESALTAR_AZUL
elif cmd == "/?cmd=7" :
mode = VisualFilters.RESALTAR_ROJO
elif cmd == "/?cmd=8" :
mode = VisualFilters.RESALTAR_VERDE
elif cmd == "/?cmd=9" :
mode = VisualFilters.RESALTAR_BLANCO
elif cmd == "/?cmd=10" :
VisualFilters.inicializarMOG()
mode = VisualFilters.DETECTAR_MOVIMIENTO
elif cmd == "/?cmd=11" :
mode = VisualFilters.RESALTAR_COLORES_FUEGO
class CamHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.endswith('.mjpg'):
self.send_response(200)
self.send_header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
self.end_headers()
while True:
try:
rc,img = capture.read()
if not rc:
continue
if mode == VisualFilters.RESALTAR_COLORES_FUEGO :
img = VisualFilters.aumentarIntensidadPorRangoDeColor(img, 0, 18, 105, 255, 183, 255)
if mode == VisualFilters.RESALTAR_BORDES:
img = VisualFilters.encontrarBordesCanny(img)
if mode == VisualFilters.DETECTAR_MOVIMIENTO:
img = VisualFilters.detectarMovimiento(img)
if mode == VisualFilters.RESALTAR_LINEAS_RECTAS:
img = VisualFilters.marcarRectas(img)
if mode == VisualFilters.RESALTAR_HUMO:
img, porc = Reconocedor_Fuego_Humo.detectar_humo(img)
if mode == VisualFilters.RESALTAR_FUEGO:
img, porc = Reconocedor_Fuego_Humo.detectar_fuego(img)
if mode == VisualFilters.RESALTAR_AZUL:
img = VisualFilters.resalteColor(img, VisualFilters.PARAMETRO_AZUL)
if mode == VisualFilters.RESALTAR_ROJO:
img = VisualFilters.resalteColor(img, VisualFilters.PARAMETRO_ROJO)
if mode == VisualFilters.RESALTAR_VERDE:
img = VisualFilters.resalteColor(img, VisualFilters.PARAMETRO_VERDE)
if mode == VisualFilters.RESALTAR_BLANCO:
img = VisualFilters.resalteColor(img, VisualFilters.PARAMETRO_BLANCO)
imgRGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
jpg = Image.fromarray(imgRGB)
tmpFile = StringIO.StringIO()
jpg.save(tmpFile,'JPEG')
self.wfile.write("--jpgboundary")
self.send_header('Content-type','image/jpeg')
self.send_header('Content-length',str(tmpFile.len))
self.send_header('Date', str(11-11-1111))
self.send_header('mime-type', "image/jpeg")
self.end_headers()
jpg.save(self.wfile,'JPEG')
time.sleep(0.05)
except KeyboardInterrupt:
break
return
if self.path.endswith('.html'):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<html><head></head><body>')
self.wfile.write('<img src="http://127.0.0.1:8080/stream.mjpg"/>')
self.wfile.write('</body></html>')
return
class CommandThread ( threading.Thread ):
def run ( self ):
server = HTTPServer(('',8081), CommandHandler)
print "command server started"
server.serve_forever()
def main():
global capture
capture = cv2.VideoCapture(0)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480);
# capture.set(cv2.cv.CV_CAP_PROP_SATURATION,0.2);
global img
try:
cmdserver = CommandThread()
cmdserver.start()
server = HTTPServer(('',8080),CamHandler)
print "streaming server started"
server.serve_forever()
except KeyboardInterrupt:
capture.release()
server.socket.close()
if __name__ == '__main__':
main()