def main(): graph, sess = load_graph(FLAGS.pre_trained_model_path) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, FLAGS.width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FLAGS.height) mp = _mp.get_context("spawn") v = mp.Value('i', 0) lock = mp.Lock() process = mp.Process(target=mario, args=(v, lock)) process.start() while True: key = cv2.waitKey(10) if key == ord("q"): break _, frame = cap.read() frame = cv2.flip(frame, 1) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes, scores, classes = detect_hands(frame, graph, sess) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) results = predict(boxes, scores, classes, FLAGS.threshold, FLAGS.width, FLAGS.height) if len(results) == 1: x_min, x_max, y_min, y_max, category = results[0] x = int((x_min + x_max) / 2) y = int((y_min + y_max) / 2) cv2.circle(frame, (x, y), 5, RED, -1) if category == "Open" and x <= FLAGS.width / 3: action = 7 # Left jump text = "Jump left" elif category == "Closed" and x <= FLAGS.width / 3: action = 6 # Left text = "Run left" elif category == "Open" and FLAGS.width / 3 < x <= 2 * FLAGS.width / 3: action = 5 # Jump text = "Jump" elif category == "Closed" and FLAGS.width / 3 < x <= 2 * FLAGS.width / 3: action = 0 # Do nothing text = "Stay" elif category == "Open" and x > 2 * FLAGS.width / 3: action = 2 # Right jump text = "Jump right" elif category == "Closed" and x > 2 * FLAGS.width / 3: action = 1 # Right text = "Run right" else: action = 0 text = "Stay" with lock: v.value = action cv2.putText(frame, "{}".format(text), (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2) overlay = frame.copy() cv2.rectangle(overlay, (0, 0), (int(FLAGS.width / 3), FLAGS.height), ORANGE, -1) cv2.rectangle(overlay, (int(2 * FLAGS.width / 3), 0), (FLAGS.width, FLAGS.height), ORANGE, -1) cv2.addWeighted(overlay, FLAGS.alpha, frame, 1 - FLAGS.alpha, 0, frame) cv2.imshow('Detection', frame) cap.release() cv2.destroyAllWindows()
def main(): graph, sess = load_graph(FLAGS.pre_trained_model_path) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, FLAGS.width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FLAGS.height) mp = _mp.get_context("spawn") v = mp.Value('i', 0) lock = mp.Lock() process = mp.Process(target=battle_city, args=(v, lock)) process.start() x_center = int(FLAGS.width / 2) y_center = int(FLAGS.height / 2) radius = int(min(FLAGS.width, FLAGS.height) / 6) while True: key = cv2.waitKey(10) if key == ord("q"): break _, frame = cap.read() frame = cv2.flip(frame, 1) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes, scores, classes = detect_hands(frame, graph, sess) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) results = predict(boxes, scores, classes, FLAGS.threshold, FLAGS.width, FLAGS.height) if len(results) == 1: x_min, x_max, y_min, y_max, category = results[0] x = int((x_min + x_max) / 2) y = int((y_min + y_max) / 2) cv2.circle(frame, (x, y), 5, RED, -1) if category == "Closed" and np.linalg.norm((x - x_center, y - y_center)) <= radius: action = 0 # Stay text = "Stay" elif category == "Closed" and is_in_triangle((x, y), [(0, 0), (FLAGS.width, 0), (x_center, y_center)]): action = 1 # Up text = "Up" elif category == "Closed" and is_in_triangle((x, y), [(0, FLAGS.height), (FLAGS.width, FLAGS.height), (x_center, y_center)]): action = 2 # Down text = "Down" elif category == "Closed" and is_in_triangle((x, y), [(0, 0), (0, FLAGS.height), (x_center, y_center)]): action = 3 # Left text = "Left" elif category == "Closed" and is_in_triangle((x, y), [(FLAGS.width, 0), (FLAGS.width, FLAGS.height), (x_center, y_center)]): action = 4 # Right text = "Right" elif category == "Open": action = 5 # Fire text = "Fire" else: action = 0 text = "Stay" with lock: v.value = action cv2.putText(frame, "{}".format(text), (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2) overlay = frame.copy() cv2.drawContours(overlay, [np.array([(0, 0), (FLAGS.width, 0), (x_center, y_center)])], 0, CYAN, -1) cv2.drawContours(overlay, [ np.array([(0, FLAGS.height), (FLAGS.width, FLAGS.height), (x_center, y_center)])], 0, CYAN, -1) cv2.drawContours(overlay, [ np.array([(0, 0), (0, FLAGS.height), (x_center, y_center)])], 0, YELLOW, -1) cv2.drawContours(overlay, [np.array([(FLAGS.width, 0), (FLAGS.width, FLAGS.height), (x_center, y_center)])], 0, YELLOW, -1) cv2.circle(overlay, (x_center, y_center), radius, BLUE, -1) cv2.addWeighted(overlay, FLAGS.alpha, frame, 1 - FLAGS.alpha, 0, frame) cv2.imshow('Detection', frame) cap.release() cv2.destroyAllWindows()
def main(): graph, sess = load_graph(FLAGS.pre_trained_model_path) cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, FLAGS.width) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, FLAGS.height) mp = _mp.get_context("spawn") v = mp.Value('i', 0) lock = mp.Lock() process = mp.Process(target=mimic, args=(v, lock)) process.start() x_center = int(FLAGS.width / 2) y_center = int(FLAGS.height / 2) radius = int(min(FLAGS.width, FLAGS.height) / 4) while True: key = cv2.waitKey(10) if key == ord("q"): break _, frame = cap.read() frame = cv2.flip(frame, 1) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes, scores, classes = detect_hands(frame, graph, sess) results = predict(boxes, scores, classes, FLAGS.threshold, FLAGS.width, FLAGS.height) text = "Oof" top_left_square_corr = np.array([(0, 0), (FLAGS.width // 3, 0), (FLAGS.width // 3, FLAGS.height // 2), (0, FLAGS.height // 2)]) bottom_left_square_corr = np.array([(0, FLAGS.height), (0, FLAGS.height // 2), (FLAGS.width // 3, FLAGS.height // 2), (FLAGS.width // 3, FLAGS.height)]) bottom_right_square_corr = np.array([ (FLAGS.width, FLAGS.height), (FLAGS.width - FLAGS.width // 3, FLAGS.height), (FLAGS.width - FLAGS.width // 4, FLAGS.height - FLAGS.height // 3), (FLAGS.width, FLAGS.height - FLAGS.height // 3) ]) top_right_square_corr = np.array([(FLAGS.width, 0), (FLAGS.width - FLAGS.width // 4, 0), (FLAGS.width - FLAGS.width // 4, FLAGS.height // 3), (FLAGS.width, FLAGS.height // 3)]) if len(results) == 1: x_min, x_max, y_min, y_max, category = results[0] x = int((x_min + x_max) / 2) y = int((y_min + y_max) / 2) cv2.circle(frame, (x, y), 10, RED, -1) if category == "Open" and np.linalg.norm( (x - x_center, y - y_center)) <= radius: action = 1 text = action elif category == "Open" and is_in_square( (x, y), top_left_square_corr): action = 3 text = action elif category == "Open" and is_in_square( (x, y), top_right_square_corr): action = 2 text = action elif category == "Closed" and is_in_square( (x, y), bottom_right_square_corr): action = 4 text = action else: action = 0 with lock: v.value = action cv2.putText(frame, "{}".format(text), (x_min, y_min - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, GREEN, 2) overlay = frame.copy() height = FLAGS.height // 3 width = FLAGS.width // 3 cv2.drawContours(overlay, [top_left_square_corr], 0, CYAN, -1) cv2.drawContours(overlay, [bottom_right_square_corr], 0, RED, -1) cv2.drawContours(overlay, [bottom_left_square_corr], 0, GREEN, -1) cv2.drawContours(overlay, [top_right_square_corr], 0, YELLOW, -1) cv2.circle(overlay, (x_center, y_center), radius, BLUE, -1) cv2.addWeighted(overlay, FLAGS.alpha, frame, 1 - FLAGS.alpha, 0, frame) cv2.imshow('Detection', frame) cap.release() cv2.destroyAllWindows()
vc.set(cv2.CAP_PROP_FRAME_WIDTH, 640) vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) count_last = 0 cv2.namedWindow("hand", flags=cv2.WINDOW_NORMAL) cv2.createTrackbar("upper", "hand", 0, 255, nothing) cv2.createTrackbar("lower", "hand", 0, 255, nothing) while (1): try: #START SEGMENTING SKIN COLOR ret, frame = vc.read() frame = cv2.flip(frame, 1) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes, scores, classes = detect_hands(frame, graph, sess) frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) results = predict(boxes, scores, classes, 0.6, 640, 480) if len(results) == 1: H = frame.shape[0] W = frame.shape[1] black = frame.copy() cv2.rectangle(black, (0, 0), (W, H), (0, 0, 0), -1) x_min, x_max, y_min, y_max, _ = results[0] crop = frame[y_min:y_max, x_min:x_max] black[y_min:y_max, x_min:x_max] = crop cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (255, 0, 0), 2) # cv2.rectangle(frame,(450,270),(452,272),(0,255,0),0)