tejovk311 commited on
Commit
b5d6210
·
verified ·
1 Parent(s): 8d58e29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +308 -302
app.py CHANGED
@@ -1,22 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  # # # # UPLOAD_FOLDER = '/tmp/received_images'
7
  # # # # os.makedirs(UPLOAD_FOLDER, exist_ok=True)
8
 
9
- # # # # latest_filename = None # Store the latest uploaded filename globally
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # # # # @app.route('/upload', methods=['POST'])
12
  # # # # def upload_file():
13
- # # # # global latest_filename
14
  # # # # img_data = request.data
15
  # # # # filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
16
  # # # # filepath = os.path.join(UPLOAD_FOLDER, filename)
17
  # # # # with open(filepath, 'wb') as f:
18
  # # # # f.write(img_data)
19
  # # # # latest_filename = filename
 
 
 
 
 
 
 
20
  # # # # return 'OK', 200
21
 
22
  # # # # @app.route('/images/<filename>')
@@ -25,33 +162,43 @@
25
 
26
  # # # # @app.route('/')
27
  # # # # def show_latest_image():
28
- # # # # global latest_filename
29
  # # # # if not latest_filename:
30
  # # # # return "<h1>No image received yet</h1>"
31
 
32
- # # # # # Auto-refresh the page every 3 seconds
33
  # # # # html = '''
34
  # # # # <html>
35
  # # # # <head>
36
- # # # # <meta http-equiv="refresh" content="3">
37
  # # # # </head>
38
  # # # # <body>
39
  # # # # <h1>Latest Image</h1>
40
  # # # # <img src="/images/{{ file }}" style="max-width:600px;"><br>
41
  # # # # <small>{{ file }}</small>
 
 
 
 
 
 
 
42
  # # # # </body>
43
  # # # # </html>
44
  # # # # '''
45
- # # # # return render_template_string(html, file=latest_filename)
46
 
47
  # # # # if __name__ == '__main__':
48
  # # # # app.run(host='0.0.0.0', port=7860)
49
 
50
 
 
 
 
51
 
52
- # # # ##########################################
53
 
54
- # # # from flask import Flask, request, send_from_directory, render_template_string
 
 
55
  # # # import os
56
  # # # from datetime import datetime
57
  # # # import cv2
@@ -60,6 +207,7 @@
60
  # # # import base64
61
  # # # import numpy as np
62
  # # # import google.generativeai as genai
 
63
 
64
  # # # # Initialize Flask app
65
  # # # app = Flask(__name__)
@@ -71,9 +219,39 @@
71
  # # # last_extraction_result = []
72
 
73
  # # # # Set your Gemini API key
74
- # # # genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4") # Replace for local or private space only
75
  # # # gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # # # def detect_and_extract_text(image_path, yolo_model_path):
78
  # # # if not os.path.exists(image_path):
79
  # # # print(f"Error: Image file not found at {image_path}")
@@ -109,23 +287,12 @@
109
  # # # _, buffer = cv2.imencode('.jpg', cropped_img_rgb)
110
  # # # image_data = base64.b64encode(buffer).decode('utf-8')
111
 
112
- # # # response = gemma_model.generate_content([
113
- # # # {
114
- # # # "role": "user",
115
- # # # "parts": [
116
- # # # {"text": "Extract the text from this image."},
117
- # # # {"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
118
- # # # ]
119
- # # # }
120
- # # # ])
121
- # # # extracted_text = response.text.strip()
122
- # # # if extracted_text.lower().startswith("the text in the image is"):
123
- # # # extracted_text = extracted_text[len("The text in the image is"):].strip().strip('"').strip("'")
124
-
125
 
126
  # # # output.append({
127
  # # # "class_name": class_name,
128
- # # # "extracted_text": extracted_text
 
129
  # # # })
130
 
131
  # # # except Exception as e:
@@ -148,7 +315,6 @@
148
  # # # f.write(img_data)
149
  # # # latest_filename = filename
150
 
151
- # # # # Perform detection + text extraction
152
  # # # try:
153
  # # # last_extraction_result = detect_and_extract_text(filepath, "best.pt")
154
  # # # except Exception as e:
@@ -160,44 +326,71 @@
160
  # # # def uploaded_file(filename):
161
  # # # return send_from_directory(UPLOAD_FOLDER, filename)
162
 
 
 
 
 
 
 
 
163
  # # # @app.route('/')
164
  # # # def show_latest_image():
165
- # # # global latest_filename, last_extraction_result
166
- # # # if not latest_filename:
167
- # # # return "<h1>No image received yet</h1>"
168
-
169
  # # # html = '''
 
170
  # # # <html>
171
  # # # <head>
172
- # # # <meta http-equiv="refresh" content="15">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  # # # </head>
174
  # # # <body>
175
  # # # <h1>Latest Image</h1>
176
- # # # <img src="/images/{{ file }}" style="max-width:600px;"><br>
177
- # # # <small>{{ file }}</small>
178
-
179
  # # # <h2>Extracted Information</h2>
180
- # # # <ul>
181
- # # # {% for item in result %}
182
- # # # <li><b>{{ item.class_name }}</b>: {{ item.extracted_text }} [{{ item.bounding_box }}]</li>
183
- # # # {% endfor %}
184
- # # # </ul>
185
  # # # </body>
186
  # # # </html>
187
  # # # '''
188
- # # # return render_template_string(html, file=latest_filename, result=last_extraction_result)
189
 
190
  # # # if __name__ == '__main__':
191
  # # # app.run(host='0.0.0.0', port=7860)
192
 
193
 
194
- # # ###########################################################################################################################################################################
195
 
196
 
197
 
198
 
199
 
200
 
 
 
 
201
  # # from flask import Flask, request, send_from_directory, render_template_string, jsonify
202
  # # import os
203
  # # from datetime import datetime
@@ -208,6 +401,7 @@
208
  # # import numpy as np
209
  # # import google.generativeai as genai
210
  # # import json
 
211
 
212
  # # # Initialize Flask app
213
  # # app = Flask(__name__)
@@ -235,18 +429,16 @@
235
  # # ])
236
  # # extracted_text = response.text.strip()
237
 
238
- # # # Try parsing JSON result
239
  # # try:
240
  # # values = json.loads(extracted_text)
241
  # # if isinstance(values, list):
242
  # # return ", ".join(values)
243
  # # except json.JSONDecodeError:
244
- # # # Fallback cleanup
245
  # # cleaned = extracted_text.strip('[]').split(',')
246
  # # cleaned = [v.strip().strip('"').strip("'") for v in cleaned]
247
  # # return ", ".join(cleaned)
248
 
249
- # # return extracted_text # Fallback raw
250
 
251
  # # except Exception as e:
252
  # # print("Error in prompt-engineered extraction:", e)
@@ -314,6 +506,7 @@
314
  # # with open(filepath, 'wb') as f:
315
  # # f.write(img_data)
316
  # # latest_filename = filename
 
317
 
318
  # # try:
319
  # # last_extraction_result = detect_and_extract_text(filepath, "best.pt")
@@ -328,6 +521,15 @@
328
 
329
  # # @app.route('/latest')
330
  # # def latest():
 
 
 
 
 
 
 
 
 
331
  # # return jsonify({
332
  # # "filename": latest_filename,
333
  # # "result": last_extraction_result
@@ -347,7 +549,8 @@
347
  # # try {
348
  # # const response = await fetch("/latest");
349
  # # const data = await response.json();
350
- # # if (data.filename && data.filename !== lastFilename) {
 
351
  # # document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
352
  # # document.getElementById("filename").innerText = data.filename;
353
 
@@ -388,7 +591,11 @@
388
 
389
 
390
 
391
- # ########################################################################################################
 
 
 
 
392
 
393
 
394
  # from flask import Flask, request, send_from_directory, render_template_string, jsonify
@@ -402,6 +609,7 @@
402
  # import google.generativeai as genai
403
  # import json
404
  # import time
 
405
 
406
  # # Initialize Flask app
407
  # app = Flask(__name__)
@@ -416,6 +624,49 @@
416
  # genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4")
417
  # gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  # def prompt_engineered_extraction(base64_image):
420
  # try:
421
  # response = gemma_model.generate_content([
@@ -481,6 +732,16 @@
481
 
482
  # extracted = prompt_engineered_extraction(image_data)
483
 
 
 
 
 
 
 
 
 
 
 
484
  # output.append({
485
  # "class_name": class_name,
486
  # "extracted_text": extracted,
@@ -544,29 +805,24 @@
544
  # <title>Live Image Monitor</title>
545
  # <script>
546
  # let lastFilename = "";
547
-
548
  # async function fetchLatest() {
549
  # try {
550
  # const response = await fetch("/latest");
551
  # const data = await response.json();
552
-
553
  # if (data.filename && data.filename !== lastFilename && data.result && data.result.length > 0) {
554
  # document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
555
  # document.getElementById("filename").innerText = data.filename;
556
-
557
  # let infoList = "";
558
  # for (const item of data.result) {
559
  # infoList += `<li><b>${item.class_name}</b>: ${item.extracted_text} [${item.bounding_box}]</li>`;
560
  # }
561
  # document.getElementById("info").innerHTML = infoList;
562
-
563
  # lastFilename = data.filename;
564
  # }
565
  # } catch (err) {
566
  # console.error("Error fetching latest data:", err);
567
  # }
568
  # }
569
-
570
  # setInterval(fetchLatest, 3000);
571
  # </script>
572
  # </head>
@@ -586,256 +842,6 @@
586
 
587
 
588
 
589
-
590
-
591
-
592
-
593
-
594
- ###############################################################################################################
595
-
596
-
597
-
598
-
599
-
600
-
601
- from flask import Flask, request, send_from_directory, render_template_string, jsonify
602
- import os
603
- from datetime import datetime
604
- import cv2
605
- import torch
606
- from ultralytics import YOLO
607
- import base64
608
- import numpy as np
609
- import google.generativeai as genai
610
- import json
611
- import time
612
  import requests
613
-
614
- # Initialize Flask app
615
- app = Flask(__name__)
616
-
617
- UPLOAD_FOLDER = '/tmp/received_images'
618
- os.makedirs(UPLOAD_FOLDER, exist_ok=True)
619
-
620
- latest_filename = None
621
- last_extraction_result = []
622
-
623
- # Set your Gemini API key
624
- genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4")
625
- gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
626
-
627
- # Supabase configuration
628
- SUPABASE_URL = "https://vynkcgoqjotnhtshbrdf.supabase.co"
629
- SUPABASE_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InZ5bmtjZ29xam90bmh0c2hicmRmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTAxMzEwMzEsImV4cCI6MjA2NTcwNzAzMX0.TEC0I2WtCMcUTt6xo5RYIHUuiOcfsJLIiYdFuUVXZI4"
630
-
631
- def store_to_supabase(values_dict):
632
- headers = {
633
- "apikey": SUPABASE_API_KEY,
634
- "Authorization": f"Bearer {SUPABASE_API_KEY}",
635
- "Content-Type": "application/json"
636
- }
637
-
638
- payload = {
639
- "heart_rate": None,
640
- "blood_pressure": None,
641
- "respiratory_rate": None,
642
- "oxygen_saturation": None
643
- }
644
-
645
- for key in payload.keys():
646
- if key in values_dict:
647
- val = values_dict[key]
648
- if key == "blood_pressure":
649
- if '/' in val and all(part.strip().isdigit() for part in val.split('/')):
650
- payload[key] = val.strip()
651
- else:
652
- if val.strip().isdigit():
653
- payload[key] = int(val.strip())
654
-
655
- for key in payload:
656
- if payload[key] is None:
657
- payload[key] = None
658
-
659
- try:
660
- response = requests.post(
661
- f"{SUPABASE_URL}/rest/v1/vitals",
662
- headers=headers,
663
- json=payload
664
- )
665
- response.raise_for_status()
666
- print("✅ Data stored in Supabase:", payload)
667
- except Exception as e:
668
- print("❌ Error storing to Supabase:", e)
669
-
670
- def prompt_engineered_extraction(base64_image):
671
- try:
672
- response = gemma_model.generate_content([
673
- {
674
- "role": "user",
675
- "parts": [
676
- {"text": "Extract only the exact text from this image. Return the result as a plain JSON array of strings like [\"value1\", \"value2\"]. Do not include any explanation, label, or formatting."},
677
- {"inline_data": {"mime_type": "image/jpeg", "data": base64_image}}
678
- ]
679
- }
680
- ])
681
- extracted_text = response.text.strip()
682
-
683
- try:
684
- values = json.loads(extracted_text)
685
- if isinstance(values, list):
686
- return ", ".join(values)
687
- except json.JSONDecodeError:
688
- cleaned = extracted_text.strip('[]').split(',')
689
- cleaned = [v.strip().strip('"').strip("'") for v in cleaned]
690
- return ", ".join(cleaned)
691
-
692
- return extracted_text
693
-
694
- except Exception as e:
695
- print("Error in prompt-engineered extraction:", e)
696
- return "Error during text extraction"
697
-
698
- def detect_and_extract_text(image_path, yolo_model_path):
699
- if not os.path.exists(image_path):
700
- print(f"Error: Image file not found at {image_path}")
701
- return []
702
-
703
- try:
704
- yolo = YOLO(yolo_model_path)
705
- except Exception as e:
706
- print(f"Error loading YOLO model: {e}")
707
- return []
708
-
709
- img = cv2.imread(image_path)
710
- if img is None:
711
- print(f"Error: Could not load image at {image_path}")
712
- return []
713
-
714
- try:
715
- results = yolo(image_path)
716
- except Exception as e:
717
- print(f"Error during YOLO inference: {e}")
718
- return []
719
-
720
- output = []
721
-
722
- for i, r in enumerate(results):
723
- for j, (box, cls) in enumerate(zip(r.boxes.xyxy, r.boxes.cls)):
724
- x1, y1, x2, y2 = map(int, box)
725
- cropped_img = img[y1:y2, x1:x2]
726
- class_name = yolo.names[int(cls)]
727
-
728
- try:
729
- cropped_img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
730
- _, buffer = cv2.imencode('.jpg', cropped_img_rgb)
731
- image_data = base64.b64encode(buffer).decode('utf-8')
732
-
733
- extracted = prompt_engineered_extraction(image_data)
734
-
735
- # Try storing to Supabase if it's a known vital
736
- vitals_key_map = {
737
- "heart_rate": "heart_rate",
738
- "blood_pressure": "blood_pressure",
739
- "respiratory_rate": "respiratory_rate",
740
- "oxygen_saturation": "oxygen_saturation"
741
- }
742
- if class_name in vitals_key_map:
743
- store_to_supabase({vitals_key_map[class_name]: extracted})
744
-
745
- output.append({
746
- "class_name": class_name,
747
- "extracted_text": extracted,
748
- "bounding_box": [x1, y1, x2, y2]
749
- })
750
-
751
- except Exception as e:
752
- print(f"Error processing object {j+1} (Class={class_name}): {e}")
753
- output.append({
754
- "class_name": class_name,
755
- "extracted_text": "Error during text extraction",
756
- "bounding_box": [x1, y1, x2, y2]
757
- })
758
-
759
- return output
760
-
761
- @app.route('/upload', methods=['POST'])
762
- def upload_file():
763
- global latest_filename, last_extraction_result
764
- img_data = request.data
765
- filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
766
- filepath = os.path.join(UPLOAD_FOLDER, filename)
767
- with open(filepath, 'wb') as f:
768
- f.write(img_data)
769
- latest_filename = filename
770
- last_extraction_result = [] # Reset before processing
771
-
772
- try:
773
- last_extraction_result = detect_and_extract_text(filepath, "best.pt")
774
- except Exception as e:
775
- last_extraction_result = [{"class_name": "Error", "extracted_text": str(e), "bounding_box": []}]
776
-
777
- return 'OK', 200
778
-
779
- @app.route('/images/<filename>')
780
- def uploaded_file(filename):
781
- return send_from_directory(UPLOAD_FOLDER, filename)
782
-
783
- @app.route('/latest')
784
- def latest():
785
- global latest_filename, last_extraction_result
786
-
787
- timeout = 10
788
- start_time = time.time()
789
-
790
- # Wait for extraction result to be ready
791
- while not last_extraction_result and (time.time() - start_time) < timeout:
792
- time.sleep(0.5)
793
-
794
- return jsonify({
795
- "filename": latest_filename,
796
- "result": last_extraction_result
797
- })
798
-
799
- @app.route('/')
800
- def show_latest_image():
801
- html = '''
802
- <!DOCTYPE html>
803
- <html>
804
- <head>
805
- <title>Live Image Monitor</title>
806
- <script>
807
- let lastFilename = "";
808
- async function fetchLatest() {
809
- try {
810
- const response = await fetch("/latest");
811
- const data = await response.json();
812
- if (data.filename && data.filename !== lastFilename && data.result && data.result.length > 0) {
813
- document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
814
- document.getElementById("filename").innerText = data.filename;
815
- let infoList = "";
816
- for (const item of data.result) {
817
- infoList += `<li><b>${item.class_name}</b>: ${item.extracted_text} [${item.bounding_box}]</li>`;
818
- }
819
- document.getElementById("info").innerHTML = infoList;
820
- lastFilename = data.filename;
821
- }
822
- } catch (err) {
823
- console.error("Error fetching latest data:", err);
824
- }
825
- }
826
- setInterval(fetchLatest, 3000);
827
- </script>
828
- </head>
829
- <body>
830
- <h1>Latest Image</h1>
831
- <img id="latest-img" src="" style="max-width:600px;"><br>
832
- <small id="filename">Waiting for image...</small>
833
- <h2>Extracted Information</h2>
834
- <ul id="info"></ul>
835
- </body>
836
- </html>
837
- '''
838
- return render_template_string(html)
839
-
840
- if __name__ == '__main__':
841
- app.run(host='0.0.0.0', port=7860)
 
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
+ # # # # # UPLOAD_FOLDER = '/tmp/received_images'
7
+ # # # # # os.makedirs(UPLOAD_FOLDER, exist_ok=True)
8
+
9
+ # # # # # latest_filename = None # Store the latest uploaded filename globally
10
+
11
+ # # # # # @app.route('/upload', methods=['POST'])
12
+ # # # # # def upload_file():
13
+ # # # # # global latest_filename
14
+ # # # # # img_data = request.data
15
+ # # # # # filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
16
+ # # # # # filepath = os.path.join(UPLOAD_FOLDER, filename)
17
+ # # # # # with open(filepath, 'wb') as f:
18
+ # # # # # f.write(img_data)
19
+ # # # # # latest_filename = filename
20
+ # # # # # return 'OK', 200
21
+
22
+ # # # # # @app.route('/images/<filename>')
23
+ # # # # # def uploaded_file(filename):
24
+ # # # # # return send_from_directory(UPLOAD_FOLDER, filename)
25
+
26
+ # # # # # @app.route('/')
27
+ # # # # # def show_latest_image():
28
+ # # # # # global latest_filename
29
+ # # # # # if not latest_filename:
30
+ # # # # # return "<h1>No image received yet</h1>"
31
+
32
+ # # # # # # Auto-refresh the page every 3 seconds
33
+ # # # # # html = '''
34
+ # # # # # <html>
35
+ # # # # # <head>
36
+ # # # # # <meta http-equiv="refresh" content="3">
37
+ # # # # # </head>
38
+ # # # # # <body>
39
+ # # # # # <h1>Latest Image</h1>
40
+ # # # # # <img src="/images/{{ file }}" style="max-width:600px;"><br>
41
+ # # # # # <small>{{ file }}</small>
42
+ # # # # # </body>
43
+ # # # # # </html>
44
+ # # # # # '''
45
+ # # # # # return render_template_string(html, file=latest_filename)
46
+
47
+ # # # # # if __name__ == '__main__':
48
+ # # # # # app.run(host='0.0.0.0', port=7860)
49
+
50
+
51
+
52
+ # # # # ##########################################
53
+
54
  # # # # from flask import Flask, request, send_from_directory, render_template_string
55
  # # # # import os
56
  # # # # from datetime import datetime
57
+ # # # # import cv2
58
+ # # # # import torch
59
+ # # # # from ultralytics import YOLO
60
+ # # # # import base64
61
+ # # # # import numpy as np
62
+ # # # # import google.generativeai as genai
63
+
64
+ # # # # # Initialize Flask app
65
  # # # # app = Flask(__name__)
66
+
67
  # # # # UPLOAD_FOLDER = '/tmp/received_images'
68
  # # # # os.makedirs(UPLOAD_FOLDER, exist_ok=True)
69
 
70
+ # # # # latest_filename = None
71
+ # # # # last_extraction_result = []
72
+
73
+ # # # # # Set your Gemini API key
74
+ # # # # genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4") # Replace for local or private space only
75
+ # # # # gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
76
+
77
+ # # # # def detect_and_extract_text(image_path, yolo_model_path):
78
+ # # # # if not os.path.exists(image_path):
79
+ # # # # print(f"Error: Image file not found at {image_path}")
80
+ # # # # return []
81
+
82
+ # # # # try:
83
+ # # # # yolo = YOLO(yolo_model_path)
84
+ # # # # except Exception as e:
85
+ # # # # print(f"Error loading YOLO model: {e}")
86
+ # # # # return []
87
+
88
+ # # # # img = cv2.imread(image_path)
89
+ # # # # if img is None:
90
+ # # # # print(f"Error: Could not load image at {image_path}")
91
+ # # # # return []
92
+
93
+ # # # # try:
94
+ # # # # results = yolo(image_path)
95
+ # # # # except Exception as e:
96
+ # # # # print(f"Error during YOLO inference: {e}")
97
+ # # # # return []
98
+
99
+ # # # # output = []
100
+
101
+ # # # # for i, r in enumerate(results):
102
+ # # # # for j, (box, cls) in enumerate(zip(r.boxes.xyxy, r.boxes.cls)):
103
+ # # # # x1, y1, x2, y2 = map(int, box)
104
+ # # # # cropped_img = img[y1:y2, x1:x2]
105
+ # # # # class_name = yolo.names[int(cls)]
106
+
107
+ # # # # try:
108
+ # # # # cropped_img_rgb = cv2.cvtColor(cropped_img, cv2.COLOR_BGR2RGB)
109
+ # # # # _, buffer = cv2.imencode('.jpg', cropped_img_rgb)
110
+ # # # # image_data = base64.b64encode(buffer).decode('utf-8')
111
+
112
+ # # # # response = gemma_model.generate_content([
113
+ # # # # {
114
+ # # # # "role": "user",
115
+ # # # # "parts": [
116
+ # # # # {"text": "Extract the text from this image."},
117
+ # # # # {"inline_data": {"mime_type": "image/jpeg", "data": image_data}}
118
+ # # # # ]
119
+ # # # # }
120
+ # # # # ])
121
+ # # # # extracted_text = response.text.strip()
122
+ # # # # if extracted_text.lower().startswith("the text in the image is"):
123
+ # # # # extracted_text = extracted_text[len("The text in the image is"):].strip().strip('"').strip("'")
124
+
125
+
126
+ # # # # output.append({
127
+ # # # # "class_name": class_name,
128
+ # # # # "extracted_text": extracted_text
129
+ # # # # })
130
+
131
+ # # # # except Exception as e:
132
+ # # # # print(f"Error processing object {j+1} (Class={class_name}): {e}")
133
+ # # # # output.append({
134
+ # # # # "class_name": class_name,
135
+ # # # # "extracted_text": "Error during text extraction",
136
+ # # # # "bounding_box": [x1, y1, x2, y2]
137
+ # # # # })
138
+
139
+ # # # # return output
140
 
141
  # # # # @app.route('/upload', methods=['POST'])
142
  # # # # def upload_file():
143
+ # # # # global latest_filename, last_extraction_result
144
  # # # # img_data = request.data
145
  # # # # filename = datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpeg"
146
  # # # # filepath = os.path.join(UPLOAD_FOLDER, filename)
147
  # # # # with open(filepath, 'wb') as f:
148
  # # # # f.write(img_data)
149
  # # # # latest_filename = filename
150
+
151
+ # # # # # Perform detection + text extraction
152
+ # # # # try:
153
+ # # # # last_extraction_result = detect_and_extract_text(filepath, "best.pt")
154
+ # # # # except Exception as e:
155
+ # # # # last_extraction_result = [{"class_name": "Error", "extracted_text": str(e), "bounding_box": []}]
156
+
157
  # # # # return 'OK', 200
158
 
159
  # # # # @app.route('/images/<filename>')
 
162
 
163
  # # # # @app.route('/')
164
  # # # # def show_latest_image():
165
+ # # # # global latest_filename, last_extraction_result
166
  # # # # if not latest_filename:
167
  # # # # return "<h1>No image received yet</h1>"
168
 
 
169
  # # # # html = '''
170
  # # # # <html>
171
  # # # # <head>
172
+ # # # # <meta http-equiv="refresh" content="15">
173
  # # # # </head>
174
  # # # # <body>
175
  # # # # <h1>Latest Image</h1>
176
  # # # # <img src="/images/{{ file }}" style="max-width:600px;"><br>
177
  # # # # <small>{{ file }}</small>
178
+
179
+ # # # # <h2>Extracted Information</h2>
180
+ # # # # <ul>
181
+ # # # # {% for item in result %}
182
+ # # # # <li><b>{{ item.class_name }}</b>: {{ item.extracted_text }} [{{ item.bounding_box }}]</li>
183
+ # # # # {% endfor %}
184
+ # # # # </ul>
185
  # # # # </body>
186
  # # # # </html>
187
  # # # # '''
188
+ # # # # return render_template_string(html, file=latest_filename, result=last_extraction_result)
189
 
190
  # # # # if __name__ == '__main__':
191
  # # # # app.run(host='0.0.0.0', port=7860)
192
 
193
 
194
+ # # # ###########################################################################################################################################################################
195
+
196
+
197
 
 
198
 
199
+
200
+
201
+ # # # from flask import Flask, request, send_from_directory, render_template_string, jsonify
202
  # # # import os
203
  # # # from datetime import datetime
204
  # # # import cv2
 
207
  # # # import base64
208
  # # # import numpy as np
209
  # # # import google.generativeai as genai
210
+ # # # import json
211
 
212
  # # # # Initialize Flask app
213
  # # # app = Flask(__name__)
 
219
  # # # last_extraction_result = []
220
 
221
  # # # # Set your Gemini API key
222
+ # # # genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4")
223
  # # # gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
224
 
225
+ # # # def prompt_engineered_extraction(base64_image):
226
+ # # # try:
227
+ # # # response = gemma_model.generate_content([
228
+ # # # {
229
+ # # # "role": "user",
230
+ # # # "parts": [
231
+ # # # {"text": "Extract only the exact text from this image. Return the result as a plain JSON array of strings like [\"value1\", \"value2\"]. Do not include any explanation, label, or formatting."},
232
+ # # # {"inline_data": {"mime_type": "image/jpeg", "data": base64_image}}
233
+ # # # ]
234
+ # # # }
235
+ # # # ])
236
+ # # # extracted_text = response.text.strip()
237
+
238
+ # # # # Try parsing JSON result
239
+ # # # try:
240
+ # # # values = json.loads(extracted_text)
241
+ # # # if isinstance(values, list):
242
+ # # # return ", ".join(values)
243
+ # # # except json.JSONDecodeError:
244
+ # # # # Fallback cleanup
245
+ # # # cleaned = extracted_text.strip('[]').split(',')
246
+ # # # cleaned = [v.strip().strip('"').strip("'") for v in cleaned]
247
+ # # # return ", ".join(cleaned)
248
+
249
+ # # # return extracted_text # Fallback raw
250
+
251
+ # # # except Exception as e:
252
+ # # # print("Error in prompt-engineered extraction:", e)
253
+ # # # return "Error during text extraction"
254
+
255
  # # # def detect_and_extract_text(image_path, yolo_model_path):
256
  # # # if not os.path.exists(image_path):
257
  # # # print(f"Error: Image file not found at {image_path}")
 
287
  # # # _, buffer = cv2.imencode('.jpg', cropped_img_rgb)
288
  # # # image_data = base64.b64encode(buffer).decode('utf-8')
289
 
290
+ # # # extracted = prompt_engineered_extraction(image_data)
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
  # # # output.append({
293
  # # # "class_name": class_name,
294
+ # # # "extracted_text": extracted,
295
+ # # # "bounding_box": [x1, y1, x2, y2]
296
  # # # })
297
 
298
  # # # except Exception as e:
 
315
  # # # f.write(img_data)
316
  # # # latest_filename = filename
317
 
 
318
  # # # try:
319
  # # # last_extraction_result = detect_and_extract_text(filepath, "best.pt")
320
  # # # except Exception as e:
 
326
  # # # def uploaded_file(filename):
327
  # # # return send_from_directory(UPLOAD_FOLDER, filename)
328
 
329
+ # # # @app.route('/latest')
330
+ # # # def latest():
331
+ # # # return jsonify({
332
+ # # # "filename": latest_filename,
333
+ # # # "result": last_extraction_result
334
+ # # # })
335
+
336
  # # # @app.route('/')
337
  # # # def show_latest_image():
 
 
 
 
338
  # # # html = '''
339
+ # # # <!DOCTYPE html>
340
  # # # <html>
341
  # # # <head>
342
+ # # # <title>Live Image Monitor</title>
343
+ # # # <script>
344
+ # # # let lastFilename = "";
345
+
346
+ # # # async function fetchLatest() {
347
+ # # # try {
348
+ # # # const response = await fetch("/latest");
349
+ # # # const data = await response.json();
350
+ # # # if (data.filename && data.filename !== lastFilename) {
351
+ # # # document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
352
+ # # # document.getElementById("filename").innerText = data.filename;
353
+
354
+ # # # let infoList = "";
355
+ # # # for (const item of data.result) {
356
+ # # # infoList += `<li><b>${item.class_name}</b>: ${item.extracted_text} [${item.bounding_box}]</li>`;
357
+ # # # }
358
+ # # # document.getElementById("info").innerHTML = infoList;
359
+
360
+ # # # lastFilename = data.filename;
361
+ # # # }
362
+ # # # } catch (err) {
363
+ # # # console.error("Error fetching latest data:", err);
364
+ # # # }
365
+ # # # }
366
+
367
+ # # # setInterval(fetchLatest, 3000);
368
+ # # # </script>
369
  # # # </head>
370
  # # # <body>
371
  # # # <h1>Latest Image</h1>
372
+ # # # <img id="latest-img" src="" style="max-width:600px;"><br>
373
+ # # # <small id="filename">Waiting for image...</small>
 
374
  # # # <h2>Extracted Information</h2>
375
+ # # # <ul id="info"></ul>
 
 
 
 
376
  # # # </body>
377
  # # # </html>
378
  # # # '''
379
+ # # # return render_template_string(html)
380
 
381
  # # # if __name__ == '__main__':
382
  # # # app.run(host='0.0.0.0', port=7860)
383
 
384
 
 
385
 
386
 
387
 
388
 
389
 
390
 
391
+ # # ########################################################################################################
392
+
393
+
394
  # # from flask import Flask, request, send_from_directory, render_template_string, jsonify
395
  # # import os
396
  # # from datetime import datetime
 
401
  # # import numpy as np
402
  # # import google.generativeai as genai
403
  # # import json
404
+ # # import time
405
 
406
  # # # Initialize Flask app
407
  # # app = Flask(__name__)
 
429
  # # ])
430
  # # extracted_text = response.text.strip()
431
 
 
432
  # # try:
433
  # # values = json.loads(extracted_text)
434
  # # if isinstance(values, list):
435
  # # return ", ".join(values)
436
  # # except json.JSONDecodeError:
 
437
  # # cleaned = extracted_text.strip('[]').split(',')
438
  # # cleaned = [v.strip().strip('"').strip("'") for v in cleaned]
439
  # # return ", ".join(cleaned)
440
 
441
+ # # return extracted_text
442
 
443
  # # except Exception as e:
444
  # # print("Error in prompt-engineered extraction:", e)
 
506
  # # with open(filepath, 'wb') as f:
507
  # # f.write(img_data)
508
  # # latest_filename = filename
509
+ # # last_extraction_result = [] # Reset before processing
510
 
511
  # # try:
512
  # # last_extraction_result = detect_and_extract_text(filepath, "best.pt")
 
521
 
522
  # # @app.route('/latest')
523
  # # def latest():
524
+ # # global latest_filename, last_extraction_result
525
+
526
+ # # timeout = 10
527
+ # # start_time = time.time()
528
+
529
+ # # # Wait for extraction result to be ready
530
+ # # while not last_extraction_result and (time.time() - start_time) < timeout:
531
+ # # time.sleep(0.5)
532
+
533
  # # return jsonify({
534
  # # "filename": latest_filename,
535
  # # "result": last_extraction_result
 
549
  # # try {
550
  # # const response = await fetch("/latest");
551
  # # const data = await response.json();
552
+
553
+ # # if (data.filename && data.filename !== lastFilename && data.result && data.result.length > 0) {
554
  # # document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
555
  # # document.getElementById("filename").innerText = data.filename;
556
 
 
591
 
592
 
593
 
594
+ # ###############################################################################################################
595
+
596
+
597
+
598
+
599
 
600
 
601
  # from flask import Flask, request, send_from_directory, render_template_string, jsonify
 
609
  # import google.generativeai as genai
610
  # import json
611
  # import time
612
+ # import requests
613
 
614
  # # Initialize Flask app
615
  # app = Flask(__name__)
 
624
  # genai.configure(api_key="AIzaSyD7aLN4NphvsPuyB6N3FwVw5Nxzv7gxzv4")
625
  # gemma_model = genai.GenerativeModel(model_name="models/gemma-3-12b-it")
626
 
627
+ # # Supabase configuration
628
+ # SUPABASE_URL = "https://vynkcgoqjotnhtshbrdf.supabase.co"
629
+ # SUPABASE_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InZ5bmtjZ29xam90bmh0c2hicmRmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTAxMzEwMzEsImV4cCI6MjA2NTcwNzAzMX0.TEC0I2WtCMcUTt6xo5RYIHUuiOcfsJLIiYdFuUVXZI4"
630
+
631
+ # def store_to_supabase(values_dict):
632
+ # headers = {
633
+ # "apikey": SUPABASE_API_KEY,
634
+ # "Authorization": f"Bearer {SUPABASE_API_KEY}",
635
+ # "Content-Type": "application/json"
636
+ # }
637
+
638
+ # payload = {
639
+ # "heart_rate": None,
640
+ # "blood_pressure": None,
641
+ # "respiratory_rate": None,
642
+ # "oxygen_saturation": None
643
+ # }
644
+
645
+ # for key in payload.keys():
646
+ # if key in values_dict:
647
+ # val = values_dict[key]
648
+ # if key == "blood_pressure":
649
+ # if '/' in val and all(part.strip().isdigit() for part in val.split('/')):
650
+ # payload[key] = val.strip()
651
+ # else:
652
+ # if val.strip().isdigit():
653
+ # payload[key] = int(val.strip())
654
+
655
+ # for key in payload:
656
+ # if payload[key] is None:
657
+ # payload[key] = None
658
+
659
+ # try:
660
+ # response = requests.post(
661
+ # f"{SUPABASE_URL}/rest/v1/vitals",
662
+ # headers=headers,
663
+ # json=payload
664
+ # )
665
+ # response.raise_for_status()
666
+ # print("✅ Data stored in Supabase:", payload)
667
+ # except Exception as e:
668
+ # print("❌ Error storing to Supabase:", e)
669
+
670
  # def prompt_engineered_extraction(base64_image):
671
  # try:
672
  # response = gemma_model.generate_content([
 
732
 
733
  # extracted = prompt_engineered_extraction(image_data)
734
 
735
+ # # Try storing to Supabase if it's a known vital
736
+ # vitals_key_map = {
737
+ # "heart_rate": "heart_rate",
738
+ # "blood_pressure": "blood_pressure",
739
+ # "respiratory_rate": "respiratory_rate",
740
+ # "oxygen_saturation": "oxygen_saturation"
741
+ # }
742
+ # if class_name in vitals_key_map:
743
+ # store_to_supabase({vitals_key_map[class_name]: extracted})
744
+
745
  # output.append({
746
  # "class_name": class_name,
747
  # "extracted_text": extracted,
 
805
  # <title>Live Image Monitor</title>
806
  # <script>
807
  # let lastFilename = "";
 
808
  # async function fetchLatest() {
809
  # try {
810
  # const response = await fetch("/latest");
811
  # const data = await response.json();
 
812
  # if (data.filename && data.filename !== lastFilename && data.result && data.result.length > 0) {
813
  # document.getElementById("latest-img").src = "/images/" + data.filename + "?t=" + new Date().getTime();
814
  # document.getElementById("filename").innerText = data.filename;
 
815
  # let infoList = "";
816
  # for (const item of data.result) {
817
  # infoList += `<li><b>${item.class_name}</b>: ${item.extracted_text} [${item.bounding_box}]</li>`;
818
  # }
819
  # document.getElementById("info").innerHTML = infoList;
 
820
  # lastFilename = data.filename;
821
  # }
822
  # } catch (err) {
823
  # console.error("Error fetching latest data:", err);
824
  # }
825
  # }
 
826
  # setInterval(fetchLatest, 3000);
827
  # </script>
828
  # </head>
 
842
 
843
 
844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
845
  import requests
846
+ response = requests.get("https://vynkcgoqjotnhtshbrdf.supabase.co")
847
+ print(response.status_code)