Guest User

Untitled

a guest
Feb 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import cv2, os, socket, numpy, pymysql, datetime, re
  4. from contextlib import closing
  5. from time import sleep, time
  6.  
  7.  
  8. class WebCamera:
  9.  
  10. ''' 初期化 '''
  11. def __init__(self, cascade, winsize):
  12. self.camera = cv2.VideoCapture(0)
  13. self.face_cascade = cv2.CascadeClassifier(cascade)
  14. self.winsize = winsize
  15.  
  16. ''' 画像データと座標を返す '''
  17. def send_frame(self):
  18. _, frame = self.camera.read()
  19.  
  20. frame = cv2.resize(frame, self.winsize)
  21. gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  22. faces = self.face_cascade.detectMultiScale(gray_img, 1.3, 5)
  23. for (x, y, w, h) in faces:
  24. cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
  25. return frame, faces
  26.  
  27. ''' メモリ上でjpg圧縮する '''
  28. def to_mat(self):
  29. frame, faces = self.send_frame()
  30. encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  31. result, encimg = cv2.imencode('.jpg', frame, encode_param)
  32. return encimg, faces
  33.  
  34. ''' テスト '''
  35. def test(self):
  36. while True:
  37. frame, faces = self.to_mat()
  38. cv2.imshow('cap', cv2.imdecode(frame, True))
  39. cv2.waitKey(1)
  40.  
  41.  
  42. class TcpClient:
  43.  
  44. ''' 初期化 '''
  45. def __init__(self, host, port):
  46. self.host = host
  47. self.port = port
  48.  
  49. ''' ソケット作成、データを送る '''
  50. def send(self, data):
  51. self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  52. with closing(self.soc) as soc:
  53. soc.connect((self.host, self.port))
  54. soc.send(data)
  55.  
  56.  
  57.  
  58. if __name__ == '__main__':
  59. print('client')
  60. img_client = TcpClient('localhost', 5555)
  61. pos_client = TcpClient('localhost', 5556)
  62. cam = WebCamera('./haarcascade_frontalface_default.xml', (800, 480))
  63.  
  64. while True:
  65. encimg, faces = cam.to_mat()
  66. if len(faces) != 0:
  67. pos_client.send(faces)
  68. img_client.send(bytearray(encimg))
Add Comment
Please, Sign In to add comment