trapezius60 commited on
Commit
03b1bda
·
verified ·
1 Parent(s): 1bd0656

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -22
app.py CHANGED
@@ -1,14 +1,21 @@
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
  from PIL import Image
5
 
6
  # ========================
7
- # Scan QR Function
8
  # ========================
9
  def scan_qr(image):
10
  if image is None:
11
- return "No image"
12
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
13
  detector = cv2.QRCodeDetector()
14
  data, bbox, _ = detector.detectAndDecode(img)
@@ -19,27 +26,16 @@ def scan_qr(image):
19
  # Gradio UI
20
  # ========================
21
  with gr.Blocks() as demo:
22
- gr.Markdown("## 📸 Scan QR → Auto GPS → Send to Google Form")
23
 
24
  qr_image = gr.Image(type="pil", label="Upload QR Code")
25
- qr_output = gr.Textbox(label="QR Code Text")
26
 
27
  scan_btn = gr.Button("Scan QR Code")
28
  scan_btn.click(scan_qr, inputs=qr_image, outputs=qr_output)
29
 
30
- # ⬇ ทำตัวแปร JS ให้เข้าถึงค่า QR ได้
31
- qr_output.change(
32
- fn=None,
33
- _js="""
34
- (value) => {
35
- window.qrValue = value; // เก็บค่า QR ไว้ให้ JS
36
- return value;
37
- }
38
- """
39
- )
40
-
41
  # ================================
42
- # HTML Button + JS GPS Sender
43
  # ================================
44
  gr.HTML("""
45
  <button id="sendBtn" style="
@@ -55,8 +51,11 @@ with gr.Blocks() as demo:
55
 
56
  <script>
57
  document.getElementById("sendBtn").onclick = function () {
 
 
 
58
 
59
- if (!window.qrValue) {
60
  alert("❗ กรุณาสแกน QR ก่อน");
61
  return;
62
  }
@@ -66,16 +65,16 @@ with gr.Blocks() as demo:
66
  const lat = pos.coords.latitude;
67
  const lng = pos.coords.longitude;
68
 
69
- // >>> ใส่ Google Form ID ของคุณ <<<
70
  const formUrl = "https://docs.google.com/forms/u/0/d/e/1lCvVNDdgnYaIh8TVHPW91S_LFhy1ep97vx1-cHqrruI/formResponse";
71
 
72
  const data = new FormData();
73
- data.append("entry.2013313277", window.qrValue); // ช่อง QR
74
- data.append("entry.286817265", lat); // ช่อง Latitude
75
- data.append("entry.518488535", lng); // ช่อง Longitude
76
 
77
  fetch(formUrl, { method: "POST", body: data })
78
- .then(() => alert("✅ ส่งข้อมูลสำเร็จ"))
79
  .catch(err => alert("❌ ERROR: " + err));
80
 
81
  });
 
1
+ #"https://docs.google.com/forms/u/0/d/e/1lCvVNDdgnYaIh8TVHPW91S_LFhy1ep97vx1-cHqrruI/formResponse";
2
+
3
+
4
+ #data.append("entry.2013313277", qrText);
5
+ #data.append("entry.286817265", lat);
6
+ #data.append("entry.518488535", lng);
7
+
8
  import gradio as gr
9
  import cv2
10
  import numpy as np
11
  from PIL import Image
12
 
13
  # ========================
14
+ # Scan QR
15
  # ========================
16
  def scan_qr(image):
17
  if image is None:
18
+ return ""
19
  img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
20
  detector = cv2.QRCodeDetector()
21
  data, bbox, _ = detector.detectAndDecode(img)
 
26
  # Gradio UI
27
  # ========================
28
  with gr.Blocks() as demo:
29
+ gr.Markdown("## 📸 Scan QR → Auto GPS → Google Form Sender")
30
 
31
  qr_image = gr.Image(type="pil", label="Upload QR Code")
32
+ qr_output = gr.Textbox(label="QR Code Text", elem_id="qr_output_box")
33
 
34
  scan_btn = gr.Button("Scan QR Code")
35
  scan_btn.click(scan_qr, inputs=qr_image, outputs=qr_output)
36
 
 
 
 
 
 
 
 
 
 
 
 
37
  # ================================
38
+ # HTML + JS Button
39
  # ================================
40
  gr.HTML("""
41
  <button id="sendBtn" style="
 
51
 
52
  <script>
53
  document.getElementById("sendBtn").onclick = function () {
54
+
55
+ // ดึงค่า QR จาก elem_id=qr_output_box
56
+ const qrText = document.querySelector('#qr_output_box textarea').value;
57
 
58
+ if (!qrText || qrText.startsWith("❌")) {
59
  alert("❗ กรุณาสแกน QR ก่อน");
60
  return;
61
  }
 
65
  const lat = pos.coords.latitude;
66
  const lng = pos.coords.longitude;
67
 
68
+ // >>> เปลี่ยนเป็นของคุณ <<<
69
  const formUrl = "https://docs.google.com/forms/u/0/d/e/1lCvVNDdgnYaIh8TVHPW91S_LFhy1ep97vx1-cHqrruI/formResponse";
70
 
71
  const data = new FormData();
72
+ data.append("entry.2013313277", qrText);
73
+ data.append("entry.286817265", lat);
74
+ data.append("entry.518488535", lng);
75
 
76
  fetch(formUrl, { method: "POST", body: data })
77
+ .then(() => alert("✅ ส่งสำเร็จ"))
78
  .catch(err => alert("❌ ERROR: " + err));
79
 
80
  });