Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import cv2, os, socket, numpy, pymysql, datetime, re
- from contextlib import closing
- from time import sleep, time
- class WebCamera:
- ''' 初期化 '''
- def __init__(self, cascade, winsize):
- self.camera = cv2.VideoCapture(0)
- self.face_cascade = cv2.CascadeClassifier(cascade)
- self.winsize = winsize
- ''' 画像データと座標を返す '''
- def send_frame(self):
- _, frame = self.camera.read()
- frame = cv2.resize(frame, self.winsize)
- gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- faces = self.face_cascade.detectMultiScale(gray_img, 1.3, 5)
- for (x, y, w, h) in faces:
- cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
- return frame, faces
- ''' メモリ上でjpg圧縮する '''
- def to_mat(self):
- frame, faces = self.send_frame()
- encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
- result, encimg = cv2.imencode('.jpg', frame, encode_param)
- return encimg, faces
- ''' テスト '''
- def test(self):
- while True:
- frame, faces = self.to_mat()
- cv2.imshow('cap', cv2.imdecode(frame, True))
- cv2.waitKey(1)
- class TcpClient:
- ''' 初期化 '''
- def __init__(self, host, port):
- self.host = host
- self.port = port
- ''' ソケット作成、データを送る '''
- def send(self, data):
- self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- with closing(self.soc) as soc:
- soc.connect((self.host, self.port))
- soc.send(data)
- if __name__ == '__main__':
- print('client')
- img_client = TcpClient('localhost', 5555)
- pos_client = TcpClient('localhost', 5556)
- cam = WebCamera('./haarcascade_frontalface_default.xml', (800, 480))
- while True:
- encimg, faces = cam.to_mat()
- if len(faces) != 0:
- pos_client.send(faces)
- img_client.send(bytearray(encimg))
Add Comment
Please, Sign In to add comment