-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·136 lines (102 loc) · 3.07 KB
/
server.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
import socket
import sys
import cv2
import pickle
import numpy as np
import struct # new
import zlib
import os
import time
from keras.models import load_model
from keras.preprocessing import image
# Another File Python
import summary
from summary import summariseTheResult
import koneksi
koneksi.savelog
# Load Model
model = load_model("/home/pandu/Documents/eksperimen/model/16jun21.h5")
os.system("clear")
# Set Connection
HOST = 'localhost'
PORT = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Socket created')
s.bind((HOST, PORT))
print('Socket bind complete')
s.listen(10)
print('Socket now listening')
conn, addr = s.accept()
data = b""
payload_size = struct.calcsize(">L")
print("payload_size: {}".format(payload_size))
# End Set connection
totalFrames = 0
noAction = 0
labels = ['1', '2', '3', '4', '5', '6', 'Tidak ada gerakan']
poseCount = np.zeros(7, dtype=int)
notResponse = 0
def showJson(poseCount, totalFrames):
hasil = {
"NumPose": poseCount,
"NumFrame": totalFrames
}
# print(poseCount)
koneksi.saveHistory(poseCount, totalFrames)
print("Berhasil memasukan ke database")
print('close')
while True:
while len(data) < payload_size:
print("Recv: {}".format(len(data)))
# If not receive data
if len(data) < payload_size:
notResponse += 1
print(notResponse)
print("tidak merespon")
# if notResponse > 500:
# showJson(poseCount, totalFrames)
# break
data += conn.recv(4096)
print("Done Recv: {}".format(len(data)))
packed_msg_size = data[:payload_size]
data = data[payload_size:]
msg_size = struct.unpack(">L", packed_msg_size)[0]
print("msg_size: {}".format(msg_size))
while len(data) < msg_size:
data += conn.recv(4096)
frame_data = data[:msg_size]
data = data[msg_size:]
frame = pickle.loads(frame_data, fix_imports=True, encoding="bytes")
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
# Mengirim Pesan dari Several
# Menerima Pesan dari klien
# message = conn.recv(1024).decode("UTF-8")
# print(message)
# Predict Image
test_image = image.img_to_array(frame)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
poseIdx = np.argmax(result, axis=1)
print("Gerakan terdeteksi gerakan :" + str(labels[np.argmax(result)]))
poseCount[poseIdx[0]] = poseCount[poseIdx[0]] + 1
totalFrames += 1
# If not action
label = labels[np.argmax(result)]
if label == "Tidak ada gerakan":
print(noAction)
noAction += 1
if noAction > 50:
showJson(poseCount, totalFrames)
break
else:
noAction = 0
cv2.imshow('ImageWindow', frame)
cv2.waitKey(1)
if cv2.waitKey(1) & 0xFF == ord('q'):
showJson(poseCount, totalFrames)
break
cv2.destroyAllWindows()
s.close()
conn.close()
# os.execv(__file__, sys.argv)
os.system("python3 /home/pandu/Documents/eksperimen/eksperimenClasify/testAnotherFile/server.py")