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_w = 220
rect_h = 52 rect_h = 52
weight = [] weights_list = [37.0, 4.8, 8.8, 2.5]
weight.append(1.0)
weight.append(1.0)
weight.append(1.0)
weight.append(1.0)
ARDUPORT = 'COM12' ARDUPORT = 'COM12'
# serial = serial.Serial(ARDUPORT, 115200, timeout=1) serial = serial.Serial(ARDUPORT, 115200, timeout=1)
test_candy_stat = [] candy_stat = []
s = {} # s = {}
s['img_idx'] = 0 # s['img_idx'] = 0
s['loaded'] = 0 # s['loaded'] = 0
s['required'] = 4 # s['required'] = 4
test_candy_stat.append(s) # candy_stat.append(s)
s = {} # s = {}
s['img_idx'] = 1 # s['img_idx'] = 1
s['loaded'] = 2 # s['loaded'] = 0
s['required'] = 2 # s['required'] = 2
test_candy_stat.append(s) # candy_stat.append(s)
s = {} # s = {}
s['img_idx'] = 2 # s['img_idx'] = 2
s['loaded'] = 1 # s['loaded'] = 0
s['required'] = 6 # s['required'] = 6
test_candy_stat.append(s) # candy_stat.append(s)
s = {} # s = {}
s['img_idx'] = 3 # s['img_idx'] = 3
s['loaded'] = 6 # s['loaded'] = 0
s['required'] = 9 # s['required'] = 9
test_candy_stat.append(s) # candy_stat.append(s)
previous_weight = 0
def new_order(params): def new_order(params):
pass candy_stat = params
def async_server_megahack(server): def async_server_megahack(server):
server.serve_forever() server.serve_forever()
@ -71,6 +69,33 @@ def async_server_megahack(server):
class RequestHandler(SimpleXMLRPCRequestHandler): class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',) 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): def draw_rectangles(img, n):
for i in range(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) 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: requestHandler=RequestHandler) as server:
server.register_introspection_functions() 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) server.register_function(new_order)
# Run the server's main loop
t = Thread(target=async_server_megahack, args=(server,)) t = Thread(target=async_server_megahack, args=(server,))
t.start() t.start()
# serial.write(b'e')
# line = serial.readline()[:-2]
# print (line)
# weight = -float(line)
# previous_weight = weight
frame_counter = 0 frame_counter = 0
weight = 0 weight = 0
canvas = np.zeros((720, 1280, 3)).astype(np.uint8) canvas = np.zeros((720, 1280, 3)).astype(np.uint8)
while True: while True:
frame_counter += 1 frame_counter += 1
if frame_counter == 75: if frame_counter == 75:
# serial.write(b'e') serial.write(b'e')
# weight = -float(serial.readline()[:-2]) weight = -float(serial.readline()[:-2])
frame_counter = 0 frame_counter = 0
print(weight) print(weight)
# check weights check_weights(candy_stat, weight)
# check game satus if check_endgame(candy_stat):
candy_stat = []
# TODO: do funny gif
ret, frame = cap.read() ret, frame = cap.read()
frame = cv2.resize(frame,None,fx=1.5, fy=1.5, interpolation = cv2.INTER_CUBIC) frame = cv2.resize(frame,None,fx=1.5, fy=1.5, interpolation = cv2.INTER_CUBIC)
canvas[0:720, 0:960, :] = frame[:] 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) cv2.imshow("window", canvas)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break

Loading…
Cancel
Save