Skip to content

Instantly share code, notes, and snippets.

@jsettlem
Created May 27, 2018 03:21
Show Gist options
  • Save jsettlem/e82600dabe4e70f663c68db504025de3 to your computer and use it in GitHub Desktop.
Save jsettlem/e82600dabe4e70f663c68db504025de3 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
import pygame
import serial
import time
import threading
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
frame = None
xp = 0
yp = 0
bI = 0
mrb = 0b00000000
isitover = False
def caploop():
global frame
global isitover
cap = cv2.VideoCapture(2)
print(cap.set(3,1280))
print(cap.set(4,720))
ret, frame = cap.read()
print (ret)
try:
while(True):
# Capture frame-by-frame
cap.get(2)
cap.get(2)
cap.get(2)
cap.get(2)
cap.get(2)
ret, frame = cap.read()
if isitover:
isitover = True
break
finally:
print("no more caps")
cap.release()
def doLoop():
global ser
global xp
global yp
global mrb
global bI
global frame
global isitover
pygame.init()
screen=pygame.display.set_mode((640,64),0,24)
pygame.display.set_caption("stupid python stupid pygame using a game engine for keyboard input")
going = False
bXs = [0,0,0,0,0]
cXs = [0,0,0,0,0]
bYs = [0,0,0,0,0]
cYs = [0,0,0,0,0]
ssums = [0]*20
sI = 0
prim = None
centering = False
actuallycentering = False
now = time.time()
ftime = time.time()
while True:
if (frame is not None):
frame2=frame.copy()
bI = (bI + 1) % 5
pressed = []
s = frame2[507:587, 1051:1235]
s = cv2.cvtColor(s, cv2.COLOR_BGR2GRAY)
ret, s = cv2.threshold(s, 200, 255, cv2.THRESH_BINARY)
sI = sI + 1
ssum = np.mean(s)
if (sI < 20):
ssums[sI] = ssum
else:
ssums.append(ssum)
ssums.pop(0)
if prim != None and centering == False and going:
#primsum = np.mean(prim)
diffo = abs(mean(ssums[0:10]) - mean(ssums[11:19])) * 1000
#diffo = ((np.sum(np.abs(np.subtract(s, prim))))/1000)
if diffo > 550:# or time.time() - cooldown > (36/60):
print("time to center!\a", diffo)
centering = False
now = time.time()
if diffo*5 > 600:
print ("wow so big\a", diffo)
prim = s.copy()
if time.time() - now > 1.3:
centering = False
blue,g,r = cv2.split(frame2)
cv2.rectangle(frame2, (453, 117), (817, 609), (0,0,0), 5)
cv2.rectangle(frame2, (453, 7), (897, 720), (0,255,0), 1)
r = cv2.GaussianBlur(r[17:720, 453:897], (31, 31), 0)
cv2.rectangle(r, (0,0), (5,55), 255, 20)
cv2.rectangle(r, (260,0), (330,40), 255, -1)
(minVal, maxVal, cLoc, maxLoc) = cv2.minMaxLoc(r)
cX, cY = cLoc
#cXs[bI], cYs[bI] = cLoc
#cX = int(mean(cXs))
#cY = int(mean(cYs))
cY = cY - 110
cv2.circle(frame2, (cX + 453, cY + 117), 30, (0, 255, 0), 4)
g = cv2.GaussianBlur(g[117:609, 453:817], (31, 31), 0)
(minVal, maxVal, bLoc, maxLoc) = cv2.minMaxLoc(g)
if centering:# and (cX - bLoc[0])**2 + (cY - bLoc[1])**2 < 35*35:#time.time() - now > 0.5:
bLoc = (40, (609-117)/2)
actuallycentering = True
else:
actuallycentering = False
bXs[bI], bYs[bI] = bLoc
bX = int(mean(bXs))
bY = int(mean(bYs))
if bX < 40:
bX = 40
cv2.circle(frame2, (bX + 453, bY + 117), 30, (0, 0, 255) if not actuallycentering else (255,0,0), 4)
if (going):
xp = max(min(bX - cX, 100), -100)
yp = max(min(bY - cY, 100), -100)
#put my hat back in the ring
if cX < 100 and xp < 0:
xp = 0
if cX > (817-453-10) and xp > 0:
xp = 0
if cY < 100 and yp < 0:
yp = 0
if cY > (609-117-90) and yp > 0:
yp = 0
#but for real
if cX > (817-453):
xp = -127
if cY < 0:
yp = 127
if cY > (609-117):
yp = -127
'''if cX - bX > 30 and cX > 100:
pressed.append("a")
if cX - bX < -30 and cX < (817-453-10):
pressed.append("d")
if cY - bY > 30 and cY > 50:
pressed.append("w")
if cY - bY < -30 and cY < (609-117-90):
pressed.append("s")'''
#put my hat back in the ring
'''if cX < 100:
pressed.append("d")
if cX > (817-453-10):
pressed.append("a")
if cY < 50:
pressed.append("s")
if cY > (609-117-90):
pressed.append("w")'''
'''output = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
s = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
circles = None#cv2.HoughCircles(s, cv2.HOUGH_GRADIENT, 1.2, 100, param2=250)
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
for (x, y, r) in circles:
cv2.circle(s, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(s, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
# Display the resulting frame'''
for i in pygame.event.get():
if i.type==pygame.QUIT:
break
if (pygame.key.get_focused()):
press=pygame.key.get_pressed()
for i in range(len(press)):
if press[i]:
pressed.append(pygame.key.name(i))
mrb = 0b00000000
if "g" in pressed:
going = True
if "space" in pressed:
mrb = mrb | 0b10000000
if "z" in pressed:
mrb = mrb | 0b01000000
if "x" in pressed:
mrb = mrb | 0b00100000
if "w" in pressed:
yp = -127
elif not going:
yp = 0
if "s" in pressed:
yp = 127
if "a" in pressed:
xp = -127
elif not going:
xp = 0
if "d" in pressed:
xp = 127
w = cv2.waitKey(1) & 0xFF
if w == ord('q'):
isitover = True
break
if w == ord('g'):
going = not going
print(pressed, mrb, xp, yp, bX, bY, cX, cY)
cv2.arrowedLine(frame2, (cX+ 453, cY+ 117), (cX + (xp // 2) + 453, cY + (yp//2) + 117), (0,0,255), 5)
cv2.imshow('frame2',frame2)
ftime = time.time()
def serialLoop():
global xp
global yp
global mrb
ser = serial.Serial("COM6", 38400, writeTimeout = 0)
try:
while True:
#print(xp, yp, mrb)
ser.write(bytearray([xp + 127, yp + 127, mrb]))
ser.read()
if (isitover):
break
finally:
print("the end")
ser.close()
cthread = threading.Thread(target=caploop)
cthread.daemon = False
cthread.start()
dthread = threading.Thread(target=doLoop)
dthread.daemon = False
dthread.start()
sthread = threading.Thread(target=serialLoop)
sthread.daemon = False
sthread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment