Added candy count code

master^2
Davide Bongiovanni 6 years ago
parent a789310c2d
commit da94ed3e15

@ -30,39 +30,37 @@ IMG_H = 720
rect_w = 220
rect_h = 52
weight = []
weight.append(1.0)
weight.append(1.0)
weight.append(1.0)
weight.append(1.0)
weights_list = [37.0, 4.8, 8.8, 2.5]
ARDUPORT = 'COM12'
# serial = serial.Serial(ARDUPORT, 115200, timeout=1)
test_candy_stat = []
s = {}
s['img_idx'] = 0
s['loaded'] = 0
s['required'] = 4
test_candy_stat.append(s)
s = {}
s['img_idx'] = 1
s['loaded'] = 2
s['required'] = 2
test_candy_stat.append(s)
s = {}
s['img_idx'] = 2
s['loaded'] = 1
s['required'] = 6
test_candy_stat.append(s)
s = {}
s['img_idx'] = 3
s['loaded'] = 6
s['required'] = 9
test_candy_stat.append(s)
serial = serial.Serial(ARDUPORT, 115200, timeout=1)
candy_stat = []
# s = {}
# s['img_idx'] = 0
# s['loaded'] = 0
# s['required'] = 4
# candy_stat.append(s)
# s = {}
# s['img_idx'] = 1
# s['loaded'] = 0
# s['required'] = 2
# candy_stat.append(s)
# s = {}
# s['img_idx'] = 2
# s['loaded'] = 0
# s['required'] = 6
# candy_stat.append(s)
# s = {}
# s['img_idx'] = 3
# s['loaded'] = 0
# s['required'] = 9
# candy_stat.append(s)
previous_weight = 0
def new_order(params):
pass
candy_stat = params
def async_server_megahack(server):
server.serve_forever()
@ -71,6 +69,33 @@ def async_server_megahack(server):
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
def check_endgame(params):
for s in candy_stat:
if s['required'] != s['loaded']:
return False
return True
def is_equal_with_tolerance(v1, v2, t):
return (v2 * (1-t) < v1) and (v2 * (1+t) > v1)
did_change = False
def check_weights(params, new_weight):
global previous_weight, did_change
weight_diff = new_weight - previous_weight
if weight_diff < 0.3:
return
elif not did_change:
did_change = True
else:
for i in range(4):
if is_equal_with_tolerance(weight_diff, weights_list[i], 0.1):
params[i]['loaded'] += 1
previous_weight = new_weight
did_change = False
break
if did_change:
did_change = False
previous_weight = new_weight
def draw_rectangles(img, n):
for i in range(n):
cv2.rectangle(img, (IMG_W - 5 - rect_w,5 + (rect_h + 5)*i), (IMG_W - 5, 5 + (rect_h + 5)*i + rect_h), (255, 255, 255), -1)
@ -90,29 +115,35 @@ with SimpleXMLRPCServer(('localhost', 8000),
requestHandler=RequestHandler) as server:
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(new_order)
# Run the server's main loop
t = Thread(target=async_server_megahack, args=(server,))
t.start()
# serial.write(b'e')
# line = serial.readline()[:-2]
# print (line)
# weight = -float(line)
# previous_weight = weight
frame_counter = 0
weight = 0
canvas = np.zeros((720, 1280, 3)).astype(np.uint8)
while True:
frame_counter += 1
if frame_counter == 75:
# serial.write(b'e')
# weight = -float(serial.readline()[:-2])
serial.write(b'e')
weight = -float(serial.readline()[:-2])
frame_counter = 0
print(weight)
# check weights
# check game satus
check_weights(candy_stat, weight)
if check_endgame(candy_stat):
candy_stat = []
# TODO: do funny gif
ret, frame = cap.read()
frame = cv2.resize(frame,None,fx=1.5, fy=1.5, interpolation = cv2.INTER_CUBIC)
canvas[0:720, 0:960, :] = frame[:]
print_stats(canvas, test_candy_stat)
if (len(candy_stat) > 0):
print_stats(canvas, candy_stat)
cv2.imshow("window", canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

Loading…
Cancel
Save