Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, jsonify
|
| 2 |
+
import base64
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Endpoint to handle the image
|
| 8 |
+
@app.route('/upload', methods=['POST'])
|
| 9 |
+
def upload_image():
|
| 10 |
+
data = request.json['image']
|
| 11 |
+
|
| 12 |
+
# Remove the prefix from the base64 string (if present)
|
| 13 |
+
if data.startswith('data:image/png;base64,'):
|
| 14 |
+
data = data[len('data:image/png;base64,'):]
|
| 15 |
+
|
| 16 |
+
# Decode the base64 string and save the image
|
| 17 |
+
img_data = base64.b64decode(data)
|
| 18 |
+
with open('captured_image.png', 'wb') as f:
|
| 19 |
+
f.write(img_data)
|
| 20 |
+
|
| 21 |
+
return jsonify({"message": "Image saved successfully!"})
|
| 22 |
+
|
| 23 |
+
# Homepage
|
| 24 |
+
@app.route('/')
|
| 25 |
+
def index():
|
| 26 |
+
return render_template('index.html')
|
| 27 |
+
|
| 28 |
+
if __name__ == '__main__':
|
| 29 |
+
app.run(debug=True)
|