def start_video_stream(host):
	if len(sys.argv) > 1:
		host = sys.argv[1]

	hoststr = 'http://' + host + '/video?x.mjpeg'
	print('Streaming ' + hoststr)

	stream = urllib.request.urlopen(hoststr)

	template = cv2.imread('test_images\\template_new.png')
	bytes_from_stream = bytes()
	i = 0
	while True:
		bytes_from_stream += stream.read(1024)
		a = bytes_from_stream.find(b'\xff\xd8')
		b = bytes_from_stream.find(b'\xff\xd9')
		if a != -1 and b != -1:
			jpg = bytes_from_stream[a:b + 2]
			bytes_from_stream = bytes_from_stream[b + 2:]
			frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
			cv2.imshow(host, frame)
			if i % 30 == 0:
				adjusted_image = get_scorecard_sift(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), template)
				if adjusted_image is not None:
					cv2.imshow("image", adjusted_image)
					cv2.waitKey(0)
					cv2.destroyAllWindows()
					all_digits, digit_flags = get_digits_from_scorecard(adjusted_image)
					predictions, prediction_flags = predict_digits(all_digits, digit_flags)
					comp_id = construct_id(predictions[0:3])
					times = construct_times(predictions[3:])
					print("Comp ID:", comp_id)
					for time in times:
						print(time)
					print(prediction_flags)
					prediction_flags_formatted = []
					# Handle ID
					for i in range(0, 2):
						prediction_flags_formatted.append([])
						if prediction_flags[i] == 1:
							prediction_flags_formatted[0].append(str(i))
					# Handle the 5 rounds
					for rounds in range(0, 4):
						prediction_flags_formatted.append([])
						for i in range(0, 6):
							if prediction_flags[3 + i + 7*rounds] == 1:
								prediction_flags_formatted[rounds + 1].append(str(i))
					print(prediction_flags_formatted)
					addInfoToDatabase(comp_id, times, prediction_flags_formatted)
					getWinners()

			# Press escape to close
			if cv2.waitKey(1) == 27:
				exit(0)
		i += 1
Exemplo n.º 2
0
def start_video_stream(host):
    if len(sys.argv) > 1:
        host = sys.argv[1]

    hoststr = 'http://' + host + '/video?x.mjpeg'
    print('Streaming ' + hoststr)

    stream = urllib.request.urlopen(hoststr)

    template = cv2.imread('test_images\\template_new.png')
    bytes_from_stream = bytes()
    i = 0
    while True:
        bytes_from_stream += stream.read(1024)
        a = bytes_from_stream.find(b'\xff\xd8')
        b = bytes_from_stream.find(b'\xff\xd9')
        if a != -1 and b != -1:
            jpg = bytes_from_stream[a:b + 2]
            bytes_from_stream = bytes_from_stream[b + 2:]
            frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),
                                 cv2.IMREAD_COLOR)
            cv2.imshow(host, frame)
            if i % 30 == 0:
                adjusted_image = get_scorecard_sift(
                    cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), template)
                if adjusted_image is not None:
                    cv2.imshow("image", adjusted_image)
                    cv2.waitKey(0)
                    cv2.destroyAllWindows()
                    all_flags = []
                    all_times = []
                    id_digits = get_id_from_scorecard(adjusted_image)
                    comp_ip, flags = construct_id(id_digits)
                    all_flags.append(flags)
                    print("Competitor id:", comp_ip)
                    for row in range(1, 6):
                        row_of_digits = get_row_of_digits_from_scorecard(
                            adjusted_image, row)
                        constructed_time, flags = construct_time(row_of_digits)
                        all_flags.append(flags)
                        all_times.append(constructed_time)
                        print(constructed_time)
                    for flag in all_flags:
                        print(flag)
                    addInfoToDatabase(comp_ip, all_times, all_flags)
                    getWinners()

            # Press escape to close
            if cv2.waitKey(1) == 27:
                exit(0)
        i += 1
# Extract the digits
all_digits, digit_flags = get_digits_from_scorecard(adjusted_image)
# Run all the extracted digits through the neural network
predictions, prediction_flags = predict_digits(all_digits, digit_flags)
# Construct the competitor IDs and the solve times for each round
comp_id = construct_id(predictions[0:3])
times = construct_times(predictions[3:])
# Print our results
print("Comp ID:", comp_id)
for time in times:
    print(time)
print(prediction_flags)

# Send the competitor ID and the solve times off to the database
# We must format our 'bad digit' flags for the database
prediction_flags_formatted = []
# Handle ID
prediction_flags_formatted.append([])
for i in range(0, 2):
    if prediction_flags[i] == 1:
        prediction_flags_formatted[0].append(str(i))
# Handle the 5 rounds
for rounds in range(0, 5):
    prediction_flags_formatted.append([])
    for i in range(0, 7):
        if prediction_flags[3 + i + 7 * rounds] == 1:
            prediction_flags_formatted[rounds + 1].append(str(i))
print(prediction_flags_formatted)
addInfoToDatabase(comp_id, times, prediction_flags_formatted)
getWinners()