-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
175 lines (153 loc) · 5.62 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
from flask import Flask, render_template, Response, make_response, request, jsonify
from camera import VideoCamera # Import the VideoCamera class from the camera module
import time
import pyaudio
import json
from dotenv import load_dotenv
import os
# load .env-Datei
load_dotenv()
HTTP_URL = "https://webcam.connect.prusa3d.com/c/snapshot"
# read as global variables
FINGERPRINTA = os.getenv("FINGERPRINTA")
TOKENA = os.getenv("TOKENA")
FINGERPRINTB = os.getenv("FINGERPRINTB")
TOKENB = os.getenv("TOKENB")
camera1_name = os.getenv("camera1_name")
camera2_name = os.getenv("camera2_name")
# Pre-source definition if auto-detect is wrong
camera1 = 0
camera2 = 1
# Automatic detection
camera1_name = "HD Pro Webcam C920"
camera2_name = "camera0"
# Initialize the Flask app
app = Flask(__name__)
fps = 0
# Render the index page
@app.route('/')
def index():
return render_template('index.html')
# Generator function for streaming video frames
def gen(camera):
global fps
while True:
frame = camera.get_frame()
if not frame == None:
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
# Endpoint for serving combined image from two cameras
@app.route('/image')
def image():
snap1 = VideoCamera(camera1, "printer", 270, True).get_frame(True)
snap2 = VideoCamera(camera2, "box", 180, True).get_frame(True)
snapsum = VideoCamera.sum_frame(snap1, snap2) # Calling sum_frame() to get the combined image
mimetype = 'image/jpeg'
return Response(snapsum, mimetype=mimetype)
@app.route('/video_feedA.mjpeg')
def video_feedA():
global genReturnA
genReturnA=gen(VideoCamera(camera1, "printer", 270, False))
if not genReturnA==None:
return Response(genReturnA, mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/shotA')
def shotA():
snap1=VideoCamera(camera1, "printer", 270, True).get_frame(True, True)
mimetype = 'image/jpeg'
return Response(snap1, mimetype=mimetype)
@app.route('/shotA_LQ')
def shotA_LQ():
snap1=VideoCamera(camera1, "printer", 270, True).get_frame(True, False)
mimetype = 'image/jpeg'
return Response(snap1, mimetype=mimetype)
@app.route('/shotB')
def shotB():
snap2=VideoCamera(camera2, "box", 180, True).get_frame(True, True)
mimetype = 'image/jpeg'
# Rückgabe der Bildantwort
return Response(snap2, mimetype=mimetype)
@app.route('/shotB_LQ')
def shotB_LQ():
snap2=VideoCamera(camera2, "box", 180, True).get_frame(True, False)
mimetype = 'image/jpeg'
# Rückgabe der Bildantwort
return Response(snap2, mimetype=mimetype)
@app.route('/video_feedB.mjpeg')
def video_feedB():
global genReturnB
genReturnB=gen(VideoCamera(camera2, "box", 180, False))
if not genReturnB==None:
return Response(genReturnB, mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/command', methods=['POST'])
def handle_command():
#print(request)
data = request.get_json()
command = data.get('command')
if command == 'startStreamingA':
print('Streaming A gestartet')
VideoCamera.setFPS(camera1, 30)
VideoCamera.setFPS(camera2, 5)
elif command == 'stopStreamingA':
VideoCamera.setFPS(camera1, 15)
VideoCamera.setFPS(camera2, 5)
print('Streaming A gestoppt')
elif command == 'startStreamingB':
print('Streaming B gestartet')
VideoCamera.setFPS(camera1, 5)
VideoCamera.setFPS(camera2, 30)
elif command == 'stopStreamingB':
VideoCamera.setFPS(camera1, 15)
VideoCamera.setFPS(camera2, 5)
print('Streaming B gestoppt')
elif command == 'startTimelapse':
VideoCamera.starttimelapse(camera1)
elif command == 'stopTimelapse':
VideoCamera.stoptimelapse(camera1)
elif command == 'renderTimelapse':
VideoCamera.renderTimelapse()
elif command == 'stopprusa':
VideoCamera.stopprusa()
elif command == 'startprusa':
print("Start prusa service")
VideoCamera.startprusa(HTTP_URL, camera1, FINGERPRINTA, TOKENA, camera2, FINGERPRINTB, TOKENB)
elif command == 'resetcam':
print("rest cam")
VideoCamera.resetcam(camera1, camera2)
elif command == 'test':
print("test")
VideoCamera.errorFrames[camera1] = 48
else:
# Unerwarteter Befehl
print('Ungültiger Befehl: ' + command)
# Sende eine Antwort zurück (optional)
return 'Befehl empfangen'
@app.route('/json/<cmd>', methods=['GET', 'POST'])
def add_message(cmd):
json = request.json
content=json
#print(cmd)
#print(content)
if content: VideoCamera.startprusa(camera1, camera2)
elif not content=="False": VideoCamera.stopprusa()
return jsonify({"cmd":cmd})
@app.route('/json')
def display_json():
# Get the camera settings JSON using the function
camera_settings_json = VideoCamera.get_camera_settings_json()
camera_settings_obj = json.loads(camera_settings_json)
# Return the JSON response
return jsonify(camera_settings_obj)
if __name__ == '__main__':
camera1=VideoCamera.get_camera(camera1_name)
camera2=VideoCamera.get_camera(camera2_name)
#cameras=(VideoCamera.list_ports()[1])
print("Kamera1: " + str(camera1) + " Kamera2: " +str(camera2))
VideoCamera.startprusa(HTTP_URL, camera1, FINGERPRINTA, TOKENA, camera2, FINGERPRINTB, TOKENB)
#VideoCamera.startprusa(camera1, camera2)
print("Computing...")
VideoCamera.renderTimelapse()
##Timelapse Autostart
#print("Start Timelapse")
#VideoCamera.starttimelapse(camera1)
print("Start Webservice")
app.run(host='0.0.0.0', threaded=True)