trapezius60 commited on
Commit
c8a04aa
·
verified ·
1 Parent(s): 719a326

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -2,67 +2,57 @@ import gradio as gr
2
  import cv2
3
  from PIL import Image
4
  import numpy as np
5
- import requests
6
 
7
- # ===== Google Form URL & Entry IDs =====
8
- FORM_URL = "https://docs.google.com/forms/u/1/d/e/1FAIpQLSd87wspuXaH0kcNZ33GOXsnCDVIFqaHQlpZin7FmSdFPJ4Q7Q/formResponse"
9
-
10
- ENTRY_QR = "entry.2013313277"
11
- ENTRY_LAT = "entry.286817265"
12
- ENTRY_LNG = "entry.518488535"
13
-
14
- # ===== Function scan QR + send GPS =====
15
- def scan_qr_send_form(image, lat, lng):
16
- # image จะเป็น PIL.Image ถ้า type="pil"
17
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
18
-
19
  detector = cv2.QRCodeDetector()
20
  data, bbox, _ = detector.detectAndDecode(img)
21
-
22
  if data:
23
- qr_text = data
24
- payload = {
25
- ENTRY_QR: qr_text,
26
- ENTRY_LAT: lat,
27
- ENTRY_LNG: lng
28
- }
29
- r = requests.post(FORM_URL, data=payload)
30
- if r.status_code == 200:
31
- return f"✅ Saved QR: {qr_text}\nLocation: {lat}, {lng}"
32
- else:
33
- return "❌ Failed to send to Google Form"
34
  else:
35
  return "❌ No QR code detected"
36
 
 
37
  with gr.Blocks() as demo:
38
- gr.Markdown("### Scan QR Code & Auto GPS Location Google Form")
39
-
40
  qr_image = gr.Image(type="pil", label="Upload QR Code Image")
41
-
42
- lat_input = gr.Textbox(label="Latitude", interactive=True, visible=False)
43
- lng_input = gr.Textbox(label="Longitude", interactive=True, visible=False)
44
- output = gr.Textbox(label="Result")
45
-
46
- btn = gr.Button("Scan & Submit")
47
- btn.click(
48
- scan_qr_send_form,
49
- inputs=[qr_image, lat_input, lng_input],
50
- outputs=output
51
  )
52
 
53
- # แทรก JS ผ่าน HTML component
54
  gr.HTML("""
55
  <script>
56
- if (navigator.geolocation) {
 
 
 
57
  navigator.geolocation.getCurrentPosition(function(position) {
58
- document.querySelectorAll('textarea[name="Latitude"]')[0].value = position.coords.latitude;
59
- document.querySelectorAll('textarea[name="Longitude"]')[0].value = position.coords.longitude;
 
 
 
 
 
 
 
 
 
 
 
60
  });
61
- } else {
62
- alert("Geolocation is not supported by this browser.");
63
  }
64
  </script>
 
65
  """)
66
 
67
-
68
  demo.launch()
 
2
  import cv2
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
+ # ===== Function scan QR =====
7
+ def scan_qr(image):
8
+ # Convert PIL image to OpenCV
 
 
 
 
 
 
 
9
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
 
10
  detector = cv2.QRCodeDetector()
11
  data, bbox, _ = detector.detectAndDecode(img)
 
12
  if data:
13
+ return data
 
 
 
 
 
 
 
 
 
 
14
  else:
15
  return "❌ No QR code detected"
16
 
17
+ # ===== Gradio UI =====
18
  with gr.Blocks() as demo:
19
+ gr.Markdown("### Upload QR Code Scan Text → GPS ส่งตรง Google Form")
20
+
21
  qr_image = gr.Image(type="pil", label="Upload QR Code Image")
22
+ qr_output = gr.Textbox(label="QR Code Text")
23
+ scan_btn = gr.Button("Scan QR Code")
24
+
25
+ scan_btn.click(
26
+ scan_qr,
27
+ inputs=[qr_image],
28
+ outputs=[qr_output]
 
 
 
29
  )
30
 
31
+ # ===== HTML + JS ส่ง GPS + QR text ไป Google Form =====
32
  gr.HTML("""
33
  <script>
34
+ function sendToForm() {
35
+ const qrText = document.querySelector('textarea[aria-label="QR Code Text"]').value;
36
+ if (!qrText) { alert("Please scan QR code first"); return; }
37
+
38
  navigator.geolocation.getCurrentPosition(function(position) {
39
+ const lat = position.coords.latitude;
40
+ const lng = position.coords.longitude;
41
+
42
+ const formUrl = "https://docs.google.com/forms/u/1/d/e/1FAIpQLSd87wspuXaH0kcNZ33GOXsnCDVIFqaHQlpZin7FmSdFPJ4Q7Q/formResponse";
43
+
44
+ const data = new FormData();
45
+ data.append("entry.2013313277", qrText); // QR text
46
+ data.append("entry.286817265", lat); // latitude
47
+ data.append("entry.518488535", lng); // longitude
48
+
49
+ fetch(formUrl, { method: "POST", body: data })
50
+ .then(() => alert("✅ QR + GPS sent to Google Form"))
51
+ .catch(err => alert("❌ Error: " + err));
52
  });
 
 
53
  }
54
  </script>
55
+ <button onclick="sendToForm()">Send QR + GPS to Google Form</button>
56
  """)
57
 
 
58
  demo.launch()