2013-09-30 22:16:17 +02:00
|
|
|
#!/bin/env python
|
|
|
|
|
2013-10-17 00:11:16 +02:00
|
|
|
# ============================================================================
|
|
|
|
# This script maps the 3D position of your hand, as detected by the electrodes
|
|
|
|
# in the RGB space, allowing you to pick a color for real.
|
|
|
|
|
|
|
|
# As all the other scripts in this repository, I release it under a very
|
|
|
|
# permissive license. To make a long story short : do whatever you want with
|
|
|
|
# this script (but try to have fun :), I don't mind. It would be cool to quote
|
|
|
|
# the origin of the script if you reuse it, but you don't have to. I'd like to
|
|
|
|
# be noticed of what you did cool with it (if you think it's worth). :)
|
|
|
|
# Ah, I almost forgot : If by chance we ever meet and you think this script is
|
|
|
|
# worth, you can buy me a soda :)
|
|
|
|
#
|
|
|
|
# Phyks
|
|
|
|
# =============================================================================
|
|
|
|
|
2013-09-30 22:16:17 +02:00
|
|
|
import serial
|
|
|
|
import pygame
|
2013-10-16 23:37:02 +02:00
|
|
|
import sys
|
|
|
|
import getopt
|
2013-09-30 22:16:17 +02:00
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
|
2013-09-30 23:59:35 +02:00
|
|
|
def compute_value(line, minimum, maximum):
|
2013-09-30 23:05:02 +02:00
|
|
|
if len(line) < 3:
|
|
|
|
return False
|
|
|
|
|
2013-10-05 17:46:40 +02:00
|
|
|
value = [0, 0, 0]
|
2013-09-30 23:05:02 +02:00
|
|
|
for i in range(3):
|
2013-10-05 17:46:40 +02:00
|
|
|
if maximum[i]-minimum[i] != 0:
|
|
|
|
value[i] = int(255*(line[i]-minimum[i])/(maximum[i]-minimum[i]))
|
|
|
|
else:
|
|
|
|
value[i] = 0
|
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
if value[i] > 255:
|
|
|
|
value[i] = 255
|
|
|
|
elif value[i] < 0:
|
|
|
|
value[i] = 0
|
|
|
|
|
2013-10-05 17:46:40 +02:00
|
|
|
return value
|
2013-09-30 23:05:02 +02:00
|
|
|
|
|
|
|
|
2013-10-16 23:37:02 +02:00
|
|
|
serial_port = "/dev/ttyACM0"
|
|
|
|
|
|
|
|
try:
|
2013-10-17 00:11:16 +02:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "hs:", ["help", "serial="])
|
2013-10-16 23:37:02 +02:00
|
|
|
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt == "-h" or opt == "-help":
|
|
|
|
print("Touchless 3D tracking with color mapping")
|
|
|
|
print("\nUsage : "+sys.argv[0]+" [OPTIONS]")
|
2013-10-17 00:11:16 +02:00
|
|
|
print("\nTrack the position of your hand in 3D and map it " +
|
|
|
|
"in RGB space")
|
2013-10-16 23:37:02 +02:00
|
|
|
print("\nOptions :")
|
|
|
|
print("\t-h (--help) \t display this help message")
|
2013-10-17 00:11:16 +02:00
|
|
|
print("\t-s (--serial) \t change serial port (default is " +
|
2013-10-16 23:37:02 +02:00
|
|
|
"/dev/tty/ACM0")
|
|
|
|
sys.exit(0)
|
|
|
|
elif opt in ("-s", "--serial"):
|
|
|
|
serial_port = arg
|
|
|
|
except getopt.GetoptError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
ser = serial.Serial(serial_port, 115200)
|
2013-09-30 22:16:17 +02:00
|
|
|
pygame.init()
|
|
|
|
|
2013-09-30 23:59:35 +02:00
|
|
|
# Keep old value to determine a mean value
|
|
|
|
value = [[0, 0, 0], [0, 0, 0]]
|
|
|
|
font = pygame.font.Font(None, 36)
|
|
|
|
|
2013-09-30 22:16:17 +02:00
|
|
|
size = width, height = 640, 480
|
|
|
|
screen = pygame.display.set_mode(size)
|
2013-09-30 23:05:02 +02:00
|
|
|
pygame.display.set_caption("Touchless 3D tracking")
|
2013-10-05 17:46:40 +02:00
|
|
|
screen.fill((0, 0, 0))
|
2013-09-30 22:16:17 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
ser.open()
|
2013-09-30 23:05:02 +02:00
|
|
|
except Exception as e:
|
|
|
|
print("Error while opening serial port : "+str(e))
|
2013-09-30 22:16:17 +02:00
|
|
|
|
|
|
|
if ser.isOpen():
|
|
|
|
try:
|
|
|
|
ser.flushInput()
|
|
|
|
ser.flushOutput()
|
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
maximum = [-1, -1, -1]
|
|
|
|
minimum = [-1, -1, -1]
|
2013-09-30 22:16:17 +02:00
|
|
|
|
|
|
|
print("Calibration :")
|
2013-10-05 17:46:40 +02:00
|
|
|
print("Press any key to launch the program when calibration is " +
|
2013-09-30 23:49:24 +02:00
|
|
|
"finished.")
|
|
|
|
# Display info in window
|
|
|
|
label = font.render("Calibration...", 1, (255, 255, 255))
|
|
|
|
label_pos = label.get_rect()
|
|
|
|
label_pos.centerx = screen.get_rect().centerx
|
|
|
|
label_pos.centery = 20
|
|
|
|
screen.blit(label, label_pos)
|
|
|
|
|
2013-09-30 22:16:17 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
|
2013-09-30 23:49:24 +02:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
for event in pygame.event.get():
|
2013-10-05 17:46:40 +02:00
|
|
|
if event.type == pygame.KEYDOWN:
|
2013-09-30 23:49:24 +02:00
|
|
|
running = False
|
2013-10-05 17:46:40 +02:00
|
|
|
continue
|
2013-09-30 23:49:24 +02:00
|
|
|
|
2013-10-05 17:46:40 +02:00
|
|
|
line = ser.readline()
|
|
|
|
line = line.decode().strip("\r\n")
|
|
|
|
line = line.split(" ")
|
|
|
|
line = [int(j or 0) for j in line]
|
2013-09-30 22:16:17 +02:00
|
|
|
|
2013-10-05 17:46:40 +02:00
|
|
|
for i in range(3):
|
|
|
|
if line[i] < minimum[i] or minimum[i] < 0:
|
|
|
|
minimum[i] = line[i]
|
|
|
|
if line[i] > maximum[i]:
|
|
|
|
maximum[i] = line[i]
|
|
|
|
|
|
|
|
print(line)
|
2013-09-30 22:16:17 +02:00
|
|
|
|
2013-09-30 23:49:24 +02:00
|
|
|
print("Running...")
|
2013-09-30 23:05:02 +02:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
# Quit if window is closed
|
|
|
|
for event in pygame.event.get():
|
2013-09-30 23:49:24 +02:00
|
|
|
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
|
|
|
|
pygame.quit()
|
2013-09-30 23:05:02 +02:00
|
|
|
running = False
|
2013-10-05 17:46:40 +02:00
|
|
|
continue
|
2013-09-30 23:05:02 +02:00
|
|
|
|
|
|
|
# Read line from serial
|
2013-10-05 17:46:40 +02:00
|
|
|
line = ser.readline()
|
|
|
|
line = line.decode().strip("\r\n")
|
|
|
|
line = line.split(" ")
|
|
|
|
line = [int(j or 0) for j in line]
|
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
# Compute the value for red
|
2013-09-30 23:59:35 +02:00
|
|
|
value[1] = value[0]
|
|
|
|
value[0] = compute_value(line, minimum, maximum)
|
2013-10-05 17:46:40 +02:00
|
|
|
value_bg = [(value[0][i] + value[1][i])/2 for i in range(3)]
|
2013-09-30 23:59:35 +02:00
|
|
|
value_text = [255 - i for i in compute_value(line, minimum,
|
|
|
|
maximum)]
|
2013-09-30 22:16:17 +02:00
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
if value is not False:
|
2013-09-30 23:59:35 +02:00
|
|
|
screen.fill((value_bg[0], value_bg[1], value_bg[2]))
|
2013-09-30 23:49:24 +02:00
|
|
|
|
|
|
|
# Display info in window
|
2013-09-30 23:59:35 +02:00
|
|
|
label = font.render("Running...", 1, (value_text[0],
|
|
|
|
value_text[1], value_text[2]))
|
2013-09-30 23:49:24 +02:00
|
|
|
label_pos = label.get_rect()
|
|
|
|
label_pos.centerx = screen.get_rect().centerx
|
|
|
|
label_pos.centery = 20
|
|
|
|
screen.blit(label, label_pos)
|
|
|
|
|
2013-09-30 23:05:02 +02:00
|
|
|
pygame.display.flip()
|
2013-09-30 22:16:17 +02:00
|
|
|
|
|
|
|
ser.close()
|
2013-09-30 23:05:02 +02:00
|
|
|
except Exception as e:
|
|
|
|
print("Error while fetching data from serial : "+str(e))
|