forked from antoinelame/GazeTracking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
158 lines (113 loc) ยท 4.74 KB
/
connect.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import Flask, render_template, url_for, request, jsonify, Response
import PyPDF2
import io
import openai
import cv2
import platform
from gaze_tracking import GazeTracking
openai.api_key = "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"
app = Flask(__name__)
gaze = GazeTracking()
webcam = cv2.VideoCapture(0)
app = Flask(__name__)
def gen_frames():
while True:
# We get a new frame from the webcam
_, frame = webcam.read()
# We send this frame to GazeTracking to analyze it
gaze.refresh(frame)
frame = gaze.annotated_frame()
text = ""
if gaze.is_blinking():
text = "Blinking"
elif gaze.is_right():
text = "Looking right"
elif gaze.is_left():
text = "Looking left"
elif gaze.is_center():
text = "Looking center"
cv2.putText(frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (147, 58, 31), 2)
left_pupil = gaze.pupil_left_coords()
right_pupil = gaze.pupil_right_coords()
cv2.putText(frame, "Left pupil: " + str(left_pupil), (90, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)
cv2.putText(frame, "Right pupil: " + str(right_pupil), (90, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)
# cv2.imshow("Demo", frame)
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
if cv2.waitKey(1) == 27:
break
#webcam.release()
##cv2.destroyAllWindows()
def makeRequest(messages):
return openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [messages]
)
def makeQuestion(article):
text = ""
pd_text = []
pd_text_row = []
article = article.split(".")
for text in range(len(article)):
pd_text_row.append(article[text])
if (text + 1) % 15 == 0:
pd_text.append("".join(pd_text_row))
pd_text_row = []
result = ""
print("ํ
์คํธ๋ฅผ gpt ๋
์์๊ฒ ์์ฝ์ํค๊ณ ์์ต๋๋ค.")
print("๊ธด ํ
์คํธ๋ 15๋ฌธ์ฅ์ ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ๋์ด gpt๊ฐ ๊ธฐ์ตํฉ๋๋ค.")
#์๊ธฐ์๊ฐ์ ๋ด์ฉ ์์ฝ. result์ ์์ฝํ ๋ด์ฉ ์ ์ฅ
for i in range(len(pd_text)):
currentText = pd_text[i]
question = {"role":"user", "content": "๋ค์ ๋ด์ฉ์ ์ฝ๊ณ ํ๊ตญ๋ง๋ก ์์ฝํด์ค.\n" + currentText}
completion = makeRequest(question)
response = completion['choices'][0]['message']['content'].strip()
result += response
print(f"{i + 1}๋ฒ์งธ ํ
์คํธ๋ฅผ gpt๋
์์ด ๊ธฐ์ตํ์ต๋๋ค.")
question = {"role":"user", "content": "๋ค์ ๊ธ์ ์ฝ๊ณ ํ์ฌ ๋ฉด์ ์ค์ด๊ณ ๋๊ฐ ๋ฉด์ ๊ด์ด๋ผ ์๊ฐํ๊ณ ํ๊ตญ์ด ์ง๋ฌธ ์ธ๊ฐ์ง๋ฅผ ๋ฐฐ์ด ํ์์ผ๋ก ๋ฐํํด์ค\n" + result}
print("gpt๊ฐ ์ง๋ฌธ์ ์์ฑ์ค์
๋๋ค.")
completion = makeRequest(question)
response = completion['choices'][0]['message']['content'].strip()
print(response)
return response
@app.route('/')
def home():
return render_template('index1.html')
@app.route('/video')
def index():
return render_template('liveCam.html')
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/interview')
def interview():
return render_template('interview.html')
@app.route('/interviewResult')
def interviewResult():
return render_template('interviewResult.html')
@app.route('/pdf', methods=['POST'])
def getPdfText():
# ํ์ผ ์ฝ๊ธฐ ์์
print("ํ์ผ์ ์ฝ๊ณ ์์ต๋๋ค")
# js๋ก ์์ฒญ ๋ฃ์ ํ์ผ์ ๊ฐ์ ธ์จ๋ค.
file = request.files['file']
# PyPDF2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ PdfReader ๋ฉ์๋๋ฅผ ํตํด ํ์ผ์ ์ฝ๋๋ค.
reader = PyPDF2.PdfReader(file)
article = ''
# ํ์ด์ง ๋ณ๋ก ์ฝ์ด์จ pdf ํ์ผ์ ํ
์คํธ๋ฅผ article ๋ณ์์ ๋ํด์ค๋ค.
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
article += page.extract_text()
# ๋ง๋ ์ ์ฒด ์์์ ํ
์คํธ๋ฅผ gpt์ ๋๊ฒจ ์ง๋ฌธ์ ์์ฑํ๋ค.
question = makeQuestion(article)
# ์์ฑ๋ ์ง๋ฌธ(string type array)์ questionArray๋ผ๋ key๊ฐ์ value๋ก ์ค์ ํ dictionary๋ฅผ ๋ง๋ ๋ค.
# ๋ง๋ค์ด์ง dictionary๋ฅผ jsonify ๋ฉ์๋๋ฅผ ์ฌ์ฉํด jsonํํ๋ก ๋ณํํ ํ ๋ฐํํด์ค๋ค.
return jsonify({"questionArray" : question})
@app.route('/test', methods=['GET'])
def getTest():
return jsonify({"hi": "hihi"})
if __name__ == '__main__':
#app.run('127.0.0.1', 5000, debug=True)
app.run(debug=True)