-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (119 loc) · 4.03 KB
/
Copy pathmain.py
File metadata and controls
140 lines (119 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#from cueballfinder import CueBallFinder
from cueballfinder2 import CueBallFinder
from yellowballfinder import YellowFinder
from redballfinder import RedFinder
from darkredballfinder import DarkRedFinder
from blueballfinder import BlueFinder
from greenballfinder import GreenFinder
from purpleballfinder import PurpleFinder
from distance import CornerDistance
import cv2
import numpy as np
import sys
from flask import Flask, render_template, request, redirect, Response
import random, json, math
import socket
app = Flask(__name__)
cueCoords = []
redCoords = []
yellowCoords = []
brownCoords = []
blueCoords = []
greenCoords = []
purpleCoords = []
@app.route("/")
def output():
return render_template("index.html")
@app.route("/getCoordinates")
def send():
global cueCoords
global yellowCoords
global redCoords
global brownCoords
global blueCoords
global greenCoords
global purpleCoords
#cue, yellow, red, brown, blue, green, purple
coordinates = [cueCoords, yellowCoords, redCoords, brownCoords, blueCoords, greenCoords, purpleCoords]
print(coordinates)
values = json.dumps(coordinates)
print(values)
return values
@app.route('/receiver', methods = ['POST'])
def main():
global cueCoords
global yellowCoords
global redCoords
global brownCoords
global blueCoords
global greenCoords
global purpleCoords
pipeline = CueBallFinder()
yellowBall = YellowFinder()
redBall = RedFinder()
brownBall = DarkRedFinder()
blueBall = BlueFinder()
greenBall = GreenFinder()
purpleBall = PurpleFinder()
cornerDistance = CornerDistance()
cap = cv2.VideoCapture(1)
leftCorner =[]
while cap.isOpened():
ret, frame = cap.read()
if(ret):
corners = cornerDistance.process(frame)
#print(corners)
# cv2.imshow('frame', frame)
cornerDist = math.sqrt(math.pow(corners[0][0]+ corners[1][0], 2)+math.pow(corners[0][1]+corners[1][1],2))
if corners[0][0]<corners[1][0]:
leftCorner = corners[0]
else:
leftCorner = corners[1]
break
while(cap.isOpened()):
ret, frame = cap.read()
if(ret):
# import pdb;pdb.set_trace()
output= pipeline.process(frame)
oneBall = yellowBall.process(frame)
threeBall = redBall.process(frame)
sevenBall = brownBall.process(frame)
twoBall = blueBall.process(frame)
sixBall = greenBall.process(frame)
fourBall = purpleBall.process(frame)
cv2.imshow('frame',frame)
#print (output)
if(len(output) != 0):
cueCoords = [output[0]-leftCorner[0],output[1]-leftCorner[1]]
if(len(oneBall) != 0):
yellowCoords = [oneBall[0]-leftCorner[0],oneBall[1]-leftCorner[1]]
if(len(threeBall) != 0):
redCoords = [threeBall[0]-leftCorner[0],threeBall[1]-leftCorner[1]]
if(len(sevenBall) != 0):
brownCoords = [sevenBall[0]-leftCorner[0],sevenBall[1]-leftCorner[1]]
if(len(twoBall) != 0):
blueCoords = [twoBall[0]-leftCorner[0],twoBall[1]-leftCorner[1]]
if(len(sixBall) != 0):
greenCoords = [sixBall[0]-leftCorner[0],sixBall[1]-leftCorner[1]]
if(len(fourBall) != 0):
purpleCoords = [fourBall[0]-leftCorner[0],fourBall[1]-leftCorner[1]]
#print (cueCoords)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# def worker():
# # read json + reply
# data = request.get_json()
# result = ''
# print(data)
# for item in data:
# print(item)
# # loop over every row
# if(item == 'x'):
# result += 'x ' + str(data.get(item)) + '\n'
# else:
# result += 'y ' + str(data.get(item)) + '\n'
# return result
if __name__ == "__main__":
app.run(host="130.215.11.31", debug=True, threaded=True)