Created
December 9, 2014 01:24
-
-
Save tyggerjai/e0eb3768c7779ccbef16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import cv2 | |
import urllib | |
import numpy as np | |
command_keys = {"a":1, | |
"d":2, | |
"w":3, | |
"s":4, | |
} | |
pi_host = '192.168.1.XXX' | |
pi_stream_port = '8080' | |
pi_UI_port = 50000 | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
s.connect((pi_host, pi_UI_port)) | |
except: | |
print("Could not connect to UI") | |
stream=urllib.urlopen("http://{}:{}/?action=stream".format(pi_host,pi_stream_port)) | |
mybytes='' | |
while True: | |
mybytes += stream.read(1024) | |
a = mybytes.find('\xff\xd8') | |
b = mybytes.find('\xff\xd9') | |
if a!=-1 and b!=-1: | |
jpg = mybytes[a:b+2] | |
mybytes= mybytes[b+2:] | |
frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR) | |
cv2.imshow('Pi Control Stream',frame) | |
#res = cv2.waitKey(0) | |
#print 'You pressed %d (0x%x), LSB: %d (%s)' % (res, res, res % 256, | |
# repr(chr(res%256)) if res%256 < 128 else '?') | |
res = cv2.waitKey(1) | |
if 256 > res > -1: | |
print(chr(res)) | |
if res == 27: | |
exit(0) | |
elif chr(res) in command_keys.keys(): | |
print("Sending data {}".format(command_keys[chr(res)])) | |
try: | |
s.send("{}".format(command_keys[chr(res)])) | |
except: | |
print("Cannot connect") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment