-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.py
53 lines (44 loc) · 1.39 KB
/
client_test.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
import cv2
import socket
import numpy as np
from os.path import exists
import os
## TCP 사용
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
## server ip, port
HOST = '~'
PORT = 13000
s.connect((HOST, PORT))
print("Connection to server successful")
# 이미지 전송 ; webcam 이미지 capture
cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G'))
if not cam.isOpened():
print("Cannot open camera")
exit()
# 이미지 속성 변경 3 = width, 4 = height
cam.set(3, 320)
cam.set(4, 240)
# 0~100에서 90의 이미지 품질로 설정 (default = 95)
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
#print('check1')
while True:
# time.sleep(5)
# 비디오의 한 프레임씩 읽는다.
# 제대로 읽으면 ret = True, 실패면 ret = False, frame에는 읽은 프레임
ret, frame = cam.read()
# print('check2')
if ret == False :
continue
# cv2. imencode(ext, img [, params])
# encode_param의 형식으로 frame을 jpg로 이미지를 인코딩한다.#
#print('check3')
result, frame = cv2.imencode('.jpg', frame, encode_param)
#print('check4')
# frame을 String 형태로 변환
data = np.array(frame)
stringData = data.tobytes()
#서버에 데이터 전송
#(str(len(stringData))).encode().ljust(16)
s.sendall((str(len(stringData))).encode().ljust(16) + stringData)
cam.release()