freenove_car/Code/Client/Video.py

104 lines
3.4 KiB
Python
Raw Normal View History

2019-09-24 21:09:34 -04:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import cv2
import socket
import io
import sys
import struct
from PIL import Image
from multiprocessing import Process
from Command import COMMAND as cmd
class VideoStreaming:
def __init__(self):
self.face_cascade = cv2.CascadeClassifier(r'haarcascade_frontalface_default.xml')
self.video_Flag=True
self.connect_Flag=False
self.face_x=0
self.face_y=0
def StartTcpClient(self,IP):
self.client_socket1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def StopTcpcClient(self):
try:
self.client_socket.shutdown(2)
self.client_socket1.shutdown(2)
self.client_socket.close()
self.client_socket1.close()
except:
pass
def IsValidImage4Bytes(self,buf):
bValid = True
if buf[6:10] in (b'JFIF', b'Exif'):
if not buf.rstrip(b'\0\r\n').endswith(b'\xff\xd9'):
bValid = False
else:
try:
Image.open(io.BytesIO(buf)).verify()
except:
bValid = False
return bValid
def face_detect(self,img):
2020-03-03 04:39:01 -05:00
if sys.platform.startswith('win') or sys.platform.startswith('darwin'):
2019-09-24 21:09:34 -04:00
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray,1.3,5)
if len(faces)>0 :
for (x,y,w,h) in faces:
self.face_x=float(x+w/2.0)
self.face_y=float(y+h/2.0)
2020-03-03 04:39:01 -05:00
img= cv2.circle(img, (int(self.face_x),int(self.face_y)), int((w+h)/4), (0, 255, 0), 2)
2019-09-24 21:09:34 -04:00
else:
self.face_x=0
self.face_y=0
2020-03-03 04:39:01 -05:00
cv2.imwrite('video.jpg',img)
2019-09-24 21:09:34 -04:00
def streaming(self,ip):
stream_bytes = b' '
try:
self.client_socket.connect((ip, 8000))
self.connection = self.client_socket.makefile('rb')
except:
#print "command port connect failed"
pass
while True:
try:
stream_bytes= self.connection.read(4)
2020-03-03 04:39:01 -05:00
leng=struct.unpack('<L', stream_bytes[:4])
2019-09-24 21:09:34 -04:00
jpg=self.connection.read(leng[0])
if self.IsValidImage4Bytes(jpg):
image = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
if self.video_Flag:
self.face_detect(image)
2019-10-15 21:26:16 -04:00
self.video_Flag=False
2020-03-03 04:39:01 -05:00
except Exception as e:
print (e)
2019-09-24 21:09:34 -04:00
break
def sendData(self,s):
if self.connect_Flag:
2020-03-03 04:39:01 -05:00
self.client_socket1.send(s.encode('utf-8'))
2019-09-24 21:09:34 -04:00
def recvData(self):
data=""
try:
2020-03-03 04:39:01 -05:00
data=self.client_socket1.recv(1024).decode('utf-8')
2019-09-24 21:09:34 -04:00
except:
pass
return data
def socket1_connect(self,ip):
try:
self.client_socket1.connect((ip, 5000))
self.connect_Flag=True
2020-03-03 04:39:01 -05:00
print ("Connecttion Successful !")
except Exception as e:
print ("Connect to server Faild!: Server IP is right? Server is opend?")
2019-09-24 21:09:34 -04:00
self.connect_Flag=False
if __name__ == '__main__':
pass