Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,21 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import uuid
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
-
app.config['UPLOAD_FOLDER'] = '
|
| 7 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@app.route('/')
|
| 10 |
def home():
|
| 11 |
return render_template_string('''
|
|
@@ -14,23 +24,13 @@ def home():
|
|
| 14 |
<head>
|
| 15 |
<title>StikkApp</title>
|
| 16 |
<style>
|
| 17 |
-
.option {
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
cursor: pointer;
|
| 25 |
-
}
|
| 26 |
-
#camera-container { display: none; position: relative; }
|
| 27 |
-
#camera-preview { width: 100%; }
|
| 28 |
-
#capture-btn {
|
| 29 |
-
position: absolute; bottom: 20px; left: 50%;
|
| 30 |
-
transform: translateX(-50%); padding: 10px 20px;
|
| 31 |
-
background: red; color: white; border: none;
|
| 32 |
-
border-radius: 50%; width: 60px; height: 60px;
|
| 33 |
-
}
|
| 34 |
#file-input { display: none; }
|
| 35 |
</style>
|
| 36 |
</head>
|
|
@@ -43,24 +43,24 @@ def home():
|
|
| 43 |
|
| 44 |
<div id="camera-container">
|
| 45 |
<video id="camera-preview" autoplay playsinline></video>
|
| 46 |
-
<button id="capture-btn" onclick="capturePhoto()">
|
| 47 |
</div>
|
| 48 |
|
| 49 |
<input type="file" id="file-input" accept="image/*">
|
| 50 |
|
| 51 |
<script>
|
| 52 |
-
//
|
| 53 |
let cameraStream;
|
| 54 |
|
| 55 |
async function startCamera() {
|
| 56 |
-
document.getElementById('camera-container').style.display = 'block';
|
| 57 |
try {
|
| 58 |
cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 59 |
video: { facingMode: 'environment' }
|
| 60 |
});
|
|
|
|
| 61 |
document.getElementById('camera-preview').srcObject = cameraStream;
|
| 62 |
} catch (err) {
|
| 63 |
-
alert("Errore
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
|
@@ -69,21 +69,31 @@ def home():
|
|
| 69 |
const canvas = document.createElement('canvas');
|
| 70 |
canvas.width = video.videoWidth;
|
| 71 |
canvas.height = video.videoHeight;
|
| 72 |
-
canvas.getContext('2d')
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
-
//
|
| 78 |
document.getElementById('file-input').addEventListener('change', function(e) {
|
| 79 |
-
if (e.target.files
|
| 80 |
const reader = new FileReader();
|
| 81 |
reader.onload = (event) => sendImage(event.target.result);
|
| 82 |
reader.readAsDataURL(e.target.files[0]);
|
| 83 |
}
|
| 84 |
});
|
| 85 |
|
| 86 |
-
//
|
| 87 |
async function pasteFromClipboard() {
|
| 88 |
try {
|
| 89 |
const items = await navigator.clipboard.read();
|
|
@@ -98,57 +108,57 @@ def home():
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
-
alert("Nessuna immagine trovata
|
| 102 |
} catch (err) {
|
| 103 |
-
alert("Errore
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
</script>
|
| 125 |
</body>
|
| 126 |
</html>
|
| 127 |
''')
|
| 128 |
|
| 129 |
-
@app.route('/uploads/<filename>')
|
| 130 |
-
def uploaded_file(filename):
|
| 131 |
-
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
| 132 |
-
|
| 133 |
@app.route('/upload', methods=['POST'])
|
| 134 |
def upload_image():
|
| 135 |
-
if not request.json or 'image' not in request.json:
|
| 136 |
-
return jsonify({'error': 'No image data'}), 400
|
| 137 |
-
|
| 138 |
try:
|
| 139 |
-
#
|
| 140 |
-
image_data =
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 142 |
filename = f"{unique_id}.jpg"
|
| 143 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 144 |
|
|
|
|
| 145 |
with open(filepath, 'wb') as f:
|
| 146 |
-
f.write(
|
| 147 |
|
| 148 |
return jsonify({
|
| 149 |
-
'image_url': f'/uploads/{filename}'
|
|
|
|
| 150 |
})
|
| 151 |
-
|
| 152 |
except Exception as e:
|
| 153 |
return jsonify({'error': str(e)}), 500
|
| 154 |
|
|
@@ -158,14 +168,24 @@ def preview():
|
|
| 158 |
return render_template_string('''
|
| 159 |
<!DOCTYPE html>
|
| 160 |
<html>
|
| 161 |
-
<head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
<body>
|
| 163 |
-
<img src="{{ img_url }}"
|
| 164 |
-
<
|
| 165 |
-
|
|
|
|
|
|
|
| 166 |
<script>
|
| 167 |
function analyze() {
|
| 168 |
-
|
|
|
|
|
|
|
| 169 |
}
|
| 170 |
</script>
|
| 171 |
</body>
|
|
@@ -178,19 +198,37 @@ def analyze():
|
|
| 178 |
return render_template_string('''
|
| 179 |
<!DOCTYPE html>
|
| 180 |
<html>
|
| 181 |
-
<head>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
<body>
|
| 183 |
-
<
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
| 185 |
<script>
|
| 186 |
-
const img = document.getElementById('target-
|
|
|
|
|
|
|
| 187 |
img.addEventListener('click', (e) => {
|
| 188 |
-
const
|
| 189 |
-
const x = e.
|
| 190 |
-
const y = e.
|
| 191 |
|
| 192 |
-
|
| 193 |
-
box.style.
|
|
|
|
| 194 |
box.style.width = '50px';
|
| 195 |
box.style.height = '50px';
|
| 196 |
box.style.display = 'block';
|
|
@@ -201,15 +239,28 @@ def analyze():
|
|
| 201 |
headers: { 'Content-Type': 'application/json' },
|
| 202 |
body: JSON.stringify({
|
| 203 |
img_url: "{{ img_url }}",
|
| 204 |
-
x:
|
| 205 |
-
y:
|
| 206 |
})
|
| 207 |
-
}).then(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
});
|
| 209 |
</script>
|
| 210 |
</body>
|
| 211 |
</html>
|
| 212 |
''', img_url=img_url)
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
if __name__ == '__main__':
|
| 215 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, send_from_directory, render_template_string
|
| 2 |
import uuid
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
| 9 |
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 10 |
|
| 11 |
+
# Servizio file uploadati
|
| 12 |
+
@app.route('/uploads/<filename>')
|
| 13 |
+
def serve_uploaded_file(filename):
|
| 14 |
+
try:
|
| 15 |
+
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
| 16 |
+
except FileNotFoundError:
|
| 17 |
+
return "File not found", 404
|
| 18 |
+
|
| 19 |
@app.route('/')
|
| 20 |
def home():
|
| 21 |
return render_template_string('''
|
|
|
|
| 24 |
<head>
|
| 25 |
<title>StikkApp</title>
|
| 26 |
<style>
|
| 27 |
+
.option { padding: 15px; margin: 10px; background: #4285f4;
|
| 28 |
+
color: white; text-align: center; border-radius: 5px; }
|
| 29 |
+
#camera-container { display: none; position: relative; max-width: 100%; }
|
| 30 |
+
#camera-preview { width: 100%; transform: scaleX(-1); }
|
| 31 |
+
#capture-btn { position: absolute; bottom: 20px; left: 50%;
|
| 32 |
+
transform: translateX(-50%); background: red;
|
| 33 |
+
width: 60px; height: 60px; border-radius: 50%; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
#file-input { display: none; }
|
| 35 |
</style>
|
| 36 |
</head>
|
|
|
|
| 43 |
|
| 44 |
<div id="camera-container">
|
| 45 |
<video id="camera-preview" autoplay playsinline></video>
|
| 46 |
+
<button id="capture-btn" onclick="capturePhoto()"></button>
|
| 47 |
</div>
|
| 48 |
|
| 49 |
<input type="file" id="file-input" accept="image/*">
|
| 50 |
|
| 51 |
<script>
|
| 52 |
+
// Gestione fotocamera
|
| 53 |
let cameraStream;
|
| 54 |
|
| 55 |
async function startCamera() {
|
|
|
|
| 56 |
try {
|
| 57 |
cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 58 |
video: { facingMode: 'environment' }
|
| 59 |
});
|
| 60 |
+
document.getElementById('camera-container').style.display = 'block';
|
| 61 |
document.getElementById('camera-preview').srcObject = cameraStream;
|
| 62 |
} catch (err) {
|
| 63 |
+
alert("Errore fotocamera: " + err.message);
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
|
|
|
| 69 |
const canvas = document.createElement('canvas');
|
| 70 |
canvas.width = video.videoWidth;
|
| 71 |
canvas.height = video.videoHeight;
|
| 72 |
+
const ctx = canvas.getContext('2d');
|
| 73 |
+
|
| 74 |
+
// Corregge l'orientamento per fotocamera posteriore
|
| 75 |
+
ctx.translate(canvas.width, 0);
|
| 76 |
+
ctx.scale(-1, 1);
|
| 77 |
+
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 78 |
+
|
| 79 |
+
// Converti in JPG di qualità
|
| 80 |
+
canvas.toBlob(blob => {
|
| 81 |
+
const reader = new FileReader();
|
| 82 |
+
reader.onload = () => sendImage(reader.result);
|
| 83 |
+
reader.readAsDataURL(blob);
|
| 84 |
+
}, 'image/jpeg', 0.85);
|
| 85 |
}
|
| 86 |
|
| 87 |
+
// Gestione galleria
|
| 88 |
document.getElementById('file-input').addEventListener('change', function(e) {
|
| 89 |
+
if (e.target.files[0]) {
|
| 90 |
const reader = new FileReader();
|
| 91 |
reader.onload = (event) => sendImage(event.target.result);
|
| 92 |
reader.readAsDataURL(e.target.files[0]);
|
| 93 |
}
|
| 94 |
});
|
| 95 |
|
| 96 |
+
// Gestione clipboard
|
| 97 |
async function pasteFromClipboard() {
|
| 98 |
try {
|
| 99 |
const items = await navigator.clipboard.read();
|
|
|
|
| 108 |
}
|
| 109 |
}
|
| 110 |
}
|
| 111 |
+
alert("Nessuna immagine trovata");
|
| 112 |
} catch (err) {
|
| 113 |
+
alert("Errore clipboard: " + err.message);
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
| 117 |
+
// Invio immagine al server
|
| 118 |
+
function sendImage(dataUrl) {
|
| 119 |
+
fetch('/upload', {
|
| 120 |
+
method: 'POST',
|
| 121 |
+
body: JSON.stringify({ image: dataUrl }),
|
| 122 |
+
headers: { 'Content-Type': 'application/json' }
|
| 123 |
+
})
|
| 124 |
+
.then(response => {
|
| 125 |
+
if (!response.ok) throw new Error('Upload failed');
|
| 126 |
+
return response.json();
|
| 127 |
+
})
|
| 128 |
+
.then(data => {
|
| 129 |
+
window.location.href = `/preview?img=${encodeURIComponent(data.image_url)}&t=${Date.now()}`;
|
| 130 |
+
})
|
| 131 |
+
.catch(error => {
|
| 132 |
+
console.error('Upload error:', error);
|
| 133 |
+
alert('Errore durante l\'upload');
|
| 134 |
+
});
|
| 135 |
+
}
|
| 136 |
</script>
|
| 137 |
</body>
|
| 138 |
</html>
|
| 139 |
''')
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
@app.route('/upload', methods=['POST'])
|
| 142 |
def upload_image():
|
|
|
|
|
|
|
|
|
|
| 143 |
try:
|
| 144 |
+
# Estrai i dati base64 dall'URL (rimuovi il prefisso)
|
| 145 |
+
image_data = request.json['image'].split(',')[1]
|
| 146 |
+
binary_data = base64.b64decode(image_data)
|
| 147 |
+
|
| 148 |
+
# Genera nome file univoco
|
| 149 |
+
unique_id = uuid.uuid4().hex
|
| 150 |
filename = f"{unique_id}.jpg"
|
| 151 |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 152 |
|
| 153 |
+
# Salva il file
|
| 154 |
with open(filepath, 'wb') as f:
|
| 155 |
+
f.write(binary_data)
|
| 156 |
|
| 157 |
return jsonify({
|
| 158 |
+
'image_url': f'/uploads/{filename}',
|
| 159 |
+
'unique_id': unique_id
|
| 160 |
})
|
| 161 |
+
|
| 162 |
except Exception as e:
|
| 163 |
return jsonify({'error': str(e)}), 500
|
| 164 |
|
|
|
|
| 168 |
return render_template_string('''
|
| 169 |
<!DOCTYPE html>
|
| 170 |
<html>
|
| 171 |
+
<head>
|
| 172 |
+
<title>Anteprima</title>
|
| 173 |
+
<style>
|
| 174 |
+
img { max-width: 100%; height: auto; }
|
| 175 |
+
button { padding: 10px 20px; margin: 10px; font-size: 16px; }
|
| 176 |
+
</style>
|
| 177 |
+
</head>
|
| 178 |
<body>
|
| 179 |
+
<img src="{{ img_url }}" id="preview-img">
|
| 180 |
+
<div>
|
| 181 |
+
<button onclick="analyze()">Analizza</button>
|
| 182 |
+
<button onclick="window.location.href='/'">Annulla</button>
|
| 183 |
+
</div>
|
| 184 |
<script>
|
| 185 |
function analyze() {
|
| 186 |
+
const img = document.getElementById('preview-img');
|
| 187 |
+
const timestamp = new Date().getTime();
|
| 188 |
+
window.location.href = `/analyze?img=${encodeURIComponent("{{ img_url }}")}&t=${timestamp}`;
|
| 189 |
}
|
| 190 |
</script>
|
| 191 |
</body>
|
|
|
|
| 198 |
return render_template_string('''
|
| 199 |
<!DOCTYPE html>
|
| 200 |
<html>
|
| 201 |
+
<head>
|
| 202 |
+
<title>Analisi</title>
|
| 203 |
+
<style>
|
| 204 |
+
#container { position: relative; }
|
| 205 |
+
#target-img { max-width: 100%; }
|
| 206 |
+
#selection-box {
|
| 207 |
+
position: absolute;
|
| 208 |
+
border: 2px solid red;
|
| 209 |
+
display: none;
|
| 210 |
+
pointer-events: none;
|
| 211 |
+
}
|
| 212 |
+
</style>
|
| 213 |
+
</head>
|
| 214 |
<body>
|
| 215 |
+
<div id="container">
|
| 216 |
+
<img src="{{ img_url }}" id="target-img">
|
| 217 |
+
<div id="selection-box"></div>
|
| 218 |
+
</div>
|
| 219 |
+
|
| 220 |
<script>
|
| 221 |
+
const img = document.getElementById('target-img');
|
| 222 |
+
const box = document.getElementById('selection-box');
|
| 223 |
+
|
| 224 |
img.addEventListener('click', (e) => {
|
| 225 |
+
const rect = img.getBoundingClientRect();
|
| 226 |
+
const x = e.clientX - rect.left;
|
| 227 |
+
const y = e.clientY - rect.top;
|
| 228 |
|
| 229 |
+
// Mostra il quadrato di selezione
|
| 230 |
+
box.style.left = `${x - 25}px`;
|
| 231 |
+
box.style.top = `${y - 25}px`;
|
| 232 |
box.style.width = '50px';
|
| 233 |
box.style.height = '50px';
|
| 234 |
box.style.display = 'block';
|
|
|
|
| 239 |
headers: { 'Content-Type': 'application/json' },
|
| 240 |
body: JSON.stringify({
|
| 241 |
img_url: "{{ img_url }}",
|
| 242 |
+
x: x,
|
| 243 |
+
y: y
|
| 244 |
})
|
| 245 |
+
}).then(response => response.json())
|
| 246 |
+
.then(data => {
|
| 247 |
+
// Gestisci la risposta qui
|
| 248 |
+
console.log(data);
|
| 249 |
+
});
|
| 250 |
});
|
| 251 |
</script>
|
| 252 |
</body>
|
| 253 |
</html>
|
| 254 |
''', img_url=img_url)
|
| 255 |
|
| 256 |
+
@app.route('/search', methods=['POST'])
|
| 257 |
+
def search():
|
| 258 |
+
data = request.json
|
| 259 |
+
# Qui implementa la logica di ricerca
|
| 260 |
+
return jsonify({
|
| 261 |
+
'result': 'success',
|
| 262 |
+
'data': data
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
if __name__ == '__main__':
|
| 266 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|