Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,158 +1,23 @@
|
|
| 1 |
-
from flask import Flask, request, send_from_directory, render_template_string
|
| 2 |
-
import os
|
| 3 |
-
from datetime import datetime
|
| 4 |
-
|
| 5 |
-
app = Flask(__name__)
|
| 6 |
-
|
| 7 |
-
UPLOAD_FOLDER = '/tmp/received_images'
|
| 8 |
-
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 9 |
-
|
| 10 |
-
latest_filename = None # Store the latest uploaded filename globally
|
| 11 |
-
|
| 12 |
-
@app.route('/upload', methods=['POST'])
|
| 13 |
-
def upload_file():
|
| 14 |
-
global latest_filename
|
| 15 |
-
img_data = request.data
|
| 16 |
-
filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
|
| 17 |
-
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 18 |
-
with open(filepath, 'wb') as f:
|
| 19 |
-
f.write(img_data)
|
| 20 |
-
latest_filename = filename
|
| 21 |
-
return 'OK', 200
|
| 22 |
-
|
| 23 |
-
@app.route('/images/<filename>')
|
| 24 |
-
def uploaded_file(filename):
|
| 25 |
-
return send_from_directory(UPLOAD_FOLDER, filename)
|
| 26 |
-
|
| 27 |
-
@app.route('/')
|
| 28 |
-
def show_latest_image():
|
| 29 |
-
global latest_filename
|
| 30 |
-
if not latest_filename:
|
| 31 |
-
return "<h1>No image received yet</h1>"
|
| 32 |
-
|
| 33 |
-
# Auto-refresh the page every 3 seconds
|
| 34 |
-
html = '''
|
| 35 |
-
<html>
|
| 36 |
-
<head>
|
| 37 |
-
<meta http-equiv="refresh" content="3">
|
| 38 |
-
</head>
|
| 39 |
-
<body>
|
| 40 |
-
<h1>Latest Image</h1>
|
| 41 |
-
<img src="/images/{{ file }}" style="max-width:600px;"><br>
|
| 42 |
-
<small>{{ file }}</small>
|
| 43 |
-
</body>
|
| 44 |
-
</html>
|
| 45 |
-
'''
|
| 46 |
-
return render_template_string(html, file=latest_filename)
|
| 47 |
-
|
| 48 |
-
if __name__ == '__main__':
|
| 49 |
-
app.run(host='0.0.0.0', port=7860)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
##########################################
|
| 54 |
-
|
| 55 |
# from flask import Flask, request, send_from_directory, render_template_string
|
| 56 |
# import os
|
| 57 |
# from datetime import datetime
|
| 58 |
-
|
| 59 |
-
# import torch
|
| 60 |
-
# from ultralytics import YOLO
|
| 61 |
-
# import base64
|
| 62 |
-
# import numpy as np
|
| 63 |
-
# import google.generativeai as genai
|
| 64 |
-
|
| 65 |
-
# # Initialize Flask app
|
| 66 |
# app = Flask(__name__)
|
| 67 |
|
| 68 |
# UPLOAD_FOLDER = '/tmp/received_images'
|
| 69 |
# os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 70 |
|
| 71 |
-
# latest_filename = None
|
| 72 |
-
# last_extraction_result = []
|
| 73 |
-
|
| 74 |
-
# # Set your Gemini API key
|
| 75 |
-
# genai.configure(api_key="YOUR_GEMINI_API_KEY") # Replace for local or private space only
|
| 76 |
-
# gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
|
| 77 |
-
|
| 78 |
-
# def detect_and_extract_text(image_path, yolo_model_path):
|
| 79 |
-
# if not os.path.exists(image_path):
|
| 80 |
-
# print(f"Error: Image file not found at {image_path}")
|
| 81 |
-
# return []
|
| 82 |
-
|
| 83 |
-
# try:
|
| 84 |
-
# yolo = YOLO(yolo_model_path)
|
| 85 |
-
# except Exception as e:
|
| 86 |
-
# print(f"Error loading YOLO model: {e}")
|
| 87 |
-
# return []
|
| 88 |
-
|
| 89 |
-
# img = cv2.imread(image_path)
|
| 90 |
-
# if img is None:
|
| 91 |
-
# print(f"Error: Could not load image at {image_path}")
|
| 92 |
-
# return []
|
| 93 |
-
|
| 94 |
-
# try:
|
| 95 |
-
# results = yolo(image_path)
|
| 96 |
-
# except Exception as e:
|
| 97 |
-
# print(f"Error during YOLO inference: {e}")
|
| 98 |
-
# return []
|
| 99 |
-
|
| 100 |
-
# output = []
|
| 101 |
-
|
| 102 |
-
# for i, r in enumerate(results):
|
| 103 |
-
# for j, (box, cls) in enumerate(zip(r.boxes.xyxy, r.boxes.cls)):
|
| 104 |
-
# x1, y1, x2, y2 = map(int, box)
|
| 105 |
-
# cropped_img = img[y1:y2, x1:x2]
|
| 106 |
-
# class_name = yolo.names[int(cls)]
|
| 107 |
-
|
| 108 |
-
# try:
|
| 109 |
-
# cropped_img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
|
| 110 |
-
# _, buffer = cv2.imencode('.jpg', cropped_img_rgb)
|
| 111 |
-
# image_data = base64.b64encode(buffer).decode('utf-8')
|
| 112 |
-
|
| 113 |
-
# response = gemma_model.generate_content([
|
| 114 |
-
# {
|
| 115 |
-
# "role": "user",
|
| 116 |
-
# "parts": [
|
| 117 |
-
# {"text": "Extract the text from this image."},
|
| 118 |
-
# {"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
|
| 119 |
-
# ]
|
| 120 |
-
# }
|
| 121 |
-
# ])
|
| 122 |
-
# extracted_text = response.text.strip()
|
| 123 |
-
|
| 124 |
-
# output.append({
|
| 125 |
-
# "class_name": class_name,
|
| 126 |
-
# "extracted_text": extracted_text,
|
| 127 |
-
# "bounding_box": [x1, y1, x2, y2]
|
| 128 |
-
# })
|
| 129 |
-
|
| 130 |
-
# except Exception as e:
|
| 131 |
-
# print(f"Error processing object {j+1} (Class={class_name}): {e}")
|
| 132 |
-
# output.append({
|
| 133 |
-
# "class_name": class_name,
|
| 134 |
-
# "extracted_text": "Error during text extraction",
|
| 135 |
-
# "bounding_box": [x1, y1, x2, y2]
|
| 136 |
-
# })
|
| 137 |
-
|
| 138 |
-
# return output
|
| 139 |
|
| 140 |
# @app.route('/upload', methods=['POST'])
|
| 141 |
# def upload_file():
|
| 142 |
-
# global latest_filename
|
| 143 |
# img_data = request.data
|
| 144 |
# filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
|
| 145 |
# filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 146 |
# with open(filepath, 'wb') as f:
|
| 147 |
# f.write(img_data)
|
| 148 |
# latest_filename = filename
|
| 149 |
-
|
| 150 |
-
# # Perform detection + text extraction
|
| 151 |
-
# try:
|
| 152 |
-
# last_extraction_result = detect_and_extract_text(filepath, "best.pt")
|
| 153 |
-
# except Exception as e:
|
| 154 |
-
# last_extraction_result = [{"class_name": "Error", "extracted_text": str(e), "bounding_box": []}]
|
| 155 |
-
|
| 156 |
# return 'OK', 200
|
| 157 |
|
| 158 |
# @app.route('/images/<filename>')
|
|
@@ -161,10 +26,11 @@ if __name__ == '__main__':
|
|
| 161 |
|
| 162 |
# @app.route('/')
|
| 163 |
# def show_latest_image():
|
| 164 |
-
# global latest_filename
|
| 165 |
# if not latest_filename:
|
| 166 |
# return "<h1>No image received yet</h1>"
|
| 167 |
|
|
|
|
| 168 |
# html = '''
|
| 169 |
# <html>
|
| 170 |
# <head>
|
|
@@ -174,17 +40,151 @@ if __name__ == '__main__':
|
|
| 174 |
# <h1>Latest Image</h1>
|
| 175 |
# <img src="/images/{{ file }}" style="max-width:600px;"><br>
|
| 176 |
# <small>{{ file }}</small>
|
| 177 |
-
|
| 178 |
-
# <h2>Extracted Information</h2>
|
| 179 |
-
# <ul>
|
| 180 |
-
# {% for item in result %}
|
| 181 |
-
# <li><b>{{ item.class_name }}</b>: {{ item.extracted_text }} [{{ item.bounding_box }}]</li>
|
| 182 |
-
# {% endfor %}
|
| 183 |
-
# </ul>
|
| 184 |
# </body>
|
| 185 |
# </html>
|
| 186 |
# '''
|
| 187 |
-
# return render_template_string(html, file=latest_filename
|
| 188 |
|
| 189 |
# if __name__ == '__main__':
|
| 190 |
# app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# from flask import Flask, request, send_from_directory, render_template_string
|
| 2 |
# import os
|
| 3 |
# from datetime import datetime
|
| 4 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# app = Flask(__name__)
|
| 6 |
|
| 7 |
# UPLOAD_FOLDER = '/tmp/received_images'
|
| 8 |
# os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 9 |
|
| 10 |
+
# latest_filename = None # Store the latest uploaded filename globally
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# @app.route('/upload', methods=['POST'])
|
| 13 |
# def upload_file():
|
| 14 |
+
# global latest_filename
|
| 15 |
# img_data = request.data
|
| 16 |
# filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
|
| 17 |
# filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 18 |
# with open(filepath, 'wb') as f:
|
| 19 |
# f.write(img_data)
|
| 20 |
# latest_filename = filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# return 'OK', 200
|
| 22 |
|
| 23 |
# @app.route('/images/<filename>')
|
|
|
|
| 26 |
|
| 27 |
# @app.route('/')
|
| 28 |
# def show_latest_image():
|
| 29 |
+
# global latest_filename
|
| 30 |
# if not latest_filename:
|
| 31 |
# return "<h1>No image received yet</h1>"
|
| 32 |
|
| 33 |
+
# # Auto-refresh the page every 3 seconds
|
| 34 |
# html = '''
|
| 35 |
# <html>
|
| 36 |
# <head>
|
|
|
|
| 40 |
# <h1>Latest Image</h1>
|
| 41 |
# <img src="/images/{{ file }}" style="max-width:600px;"><br>
|
| 42 |
# <small>{{ file }}</small>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# </body>
|
| 44 |
# </html>
|
| 45 |
# '''
|
| 46 |
+
# return render_template_string(html, file=latest_filename)
|
| 47 |
|
| 48 |
# if __name__ == '__main__':
|
| 49 |
# app.run(host='0.0.0.0', port=7860)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
##########################################
|
| 54 |
+
|
| 55 |
+
from flask import Flask, request, send_from_directory, render_template_string
|
| 56 |
+
import os
|
| 57 |
+
from datetime import datetime
|
| 58 |
+
import cv2
|
| 59 |
+
import torch
|
| 60 |
+
from ultralytics import YOLO
|
| 61 |
+
import base64
|
| 62 |
+
import numpy as np
|
| 63 |
+
import google.generativeai as genai
|
| 64 |
+
|
| 65 |
+
# Initialize Flask app
|
| 66 |
+
app = Flask(__name__)
|
| 67 |
+
|
| 68 |
+
UPLOAD_FOLDER = '/tmp/received_images'
|
| 69 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 70 |
+
|
| 71 |
+
latest_filename = None
|
| 72 |
+
last_extraction_result = []
|
| 73 |
+
|
| 74 |
+
# Set your Gemini API key
|
| 75 |
+
genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4") # Replace for local or private space only
|
| 76 |
+
gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
|
| 77 |
+
|
| 78 |
+
def detect_and_extract_text(image_path, yolo_model_path):
|
| 79 |
+
if not os.path.exists(image_path):
|
| 80 |
+
print(f"Error: Image file not found at {image_path}")
|
| 81 |
+
return []
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
yolo = YOLO(yolo_model_path)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"Error loading YOLO model: {e}")
|
| 87 |
+
return []
|
| 88 |
+
|
| 89 |
+
img = cv2.imread(image_path)
|
| 90 |
+
if img is None:
|
| 91 |
+
print(f"Error: Could not load image at {image_path}")
|
| 92 |
+
return []
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
results = yolo(image_path)
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"Error during YOLO inference: {e}")
|
| 98 |
+
return []
|
| 99 |
+
|
| 100 |
+
output = []
|
| 101 |
+
|
| 102 |
+
for i, r in enumerate(results):
|
| 103 |
+
for j, (box, cls) in enumerate(zip(r.boxes.xyxy, r.boxes.cls)):
|
| 104 |
+
x1, y1, x2, y2 = map(int, box)
|
| 105 |
+
cropped_img = img[y1:y2, x1:x2]
|
| 106 |
+
class_name = yolo.names[int(cls)]
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
cropped_img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
|
| 110 |
+
_, buffer = cv2.imencode('.jpg', cropped_img_rgb)
|
| 111 |
+
image_data = base64.b64encode(buffer).decode('utf-8')
|
| 112 |
+
|
| 113 |
+
response = gemma_model.generate_content([
|
| 114 |
+
{
|
| 115 |
+
"role": "user",
|
| 116 |
+
"parts": [
|
| 117 |
+
{"text": "Extract the text from this image."},
|
| 118 |
+
{"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
|
| 119 |
+
]
|
| 120 |
+
}
|
| 121 |
+
])
|
| 122 |
+
extracted_text = response.text.strip()
|
| 123 |
+
|
| 124 |
+
output.append({
|
| 125 |
+
"class_name": class_name,
|
| 126 |
+
"extracted_text": extracted_text,
|
| 127 |
+
"bounding_box": [x1, y1, x2, y2]
|
| 128 |
+
})
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"Error processing object {j+1} (Class={class_name}): {e}")
|
| 132 |
+
output.append({
|
| 133 |
+
"class_name": class_name,
|
| 134 |
+
"extracted_text": "Error during text extraction",
|
| 135 |
+
"bounding_box": [x1, y1, x2, y2]
|
| 136 |
+
})
|
| 137 |
+
|
| 138 |
+
return output
|
| 139 |
+
|
| 140 |
+
@app.route('/upload', methods=['POST'])
|
| 141 |
+
def upload_file():
|
| 142 |
+
global latest_filename, last_extraction_result
|
| 143 |
+
img_data = request.data
|
| 144 |
+
filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
|
| 145 |
+
filepath = os.path.join(UPLOAD_FOLDER, filename)
|
| 146 |
+
with open(filepath, 'wb') as f:
|
| 147 |
+
f.write(img_data)
|
| 148 |
+
latest_filename = filename
|
| 149 |
+
|
| 150 |
+
# Perform detection + text extraction
|
| 151 |
+
try:
|
| 152 |
+
last_extraction_result = detect_and_extract_text(filepath, "best.pt")
|
| 153 |
+
except Exception as e:
|
| 154 |
+
last_extraction_result = [{"class_name": "Error", "extracted_text": str(e), "bounding_box": []}]
|
| 155 |
+
|
| 156 |
+
return 'OK', 200
|
| 157 |
+
|
| 158 |
+
@app.route('/images/<filename>')
|
| 159 |
+
def uploaded_file(filename):
|
| 160 |
+
return send_from_directory(UPLOAD_FOLDER, filename)
|
| 161 |
+
|
| 162 |
+
@app.route('/')
|
| 163 |
+
def show_latest_image():
|
| 164 |
+
global latest_filename, last_extraction_result
|
| 165 |
+
if not latest_filename:
|
| 166 |
+
return "<h1>No image received yet</h1>"
|
| 167 |
+
|
| 168 |
+
html = '''
|
| 169 |
+
<html>
|
| 170 |
+
<head>
|
| 171 |
+
<meta http-equiv="refresh" content="3">
|
| 172 |
+
</head>
|
| 173 |
+
<body>
|
| 174 |
+
<h1>Latest Image</h1>
|
| 175 |
+
<img src="/images/{{ file }}" style="max-width:600px;"><br>
|
| 176 |
+
<small>{{ file }}</small>
|
| 177 |
+
|
| 178 |
+
<h2>Extracted Information</h2>
|
| 179 |
+
<ul>
|
| 180 |
+
{% for item in result %}
|
| 181 |
+
<li><b>{{ item.class_name }}</b>: {{ item.extracted_text }} [{{ item.bounding_box }}]</li>
|
| 182 |
+
{% endfor %}
|
| 183 |
+
</ul>
|
| 184 |
+
</body>
|
| 185 |
+
</html>
|
| 186 |
+
'''
|
| 187 |
+
return render_template_string(html, file=latest_filename, result=last_extraction_result)
|
| 188 |
+
|
| 189 |
+
if __name__ == '__main__':
|
| 190 |
+
app.run(host='0.0.0.0', port=7860)
|