-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesp32cam_fix.py
More file actions
198 lines (153 loc) · 6.09 KB
/
esp32cam_fix.py
File metadata and controls
198 lines (153 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import time
from datetime import datetime
from supabase import create_client, Client
import httpx
import cv2
import numpy as np
from ultralytics import YOLO
from collections import Counter
# --- KONFIGURASI UTAMA ---
SUPABASE_URL = "https://wxkoqtcvkwduzfejtxib.supabase.co"
SUPABASE_SERVICE_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Ind4a29xdGN2a3dkdXpmZWp0eGliIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc2MDIzNzcyNCwiZXhwIjoyMDc1ODEzNzI0fQ.ZAJBuiPV_FsNIQwiK40wZQ1vWLXzs-fMxFTEmYJUsqc"
BUCKET_NAME = "gambar-hasil-deteksi"
YOLO_MODEL_PATH = "best_final.pt"
CHECK_INTERVAL = 5
COLOR_MAP = {
"daun_sehat": (0, 255, 0),
"daun_kuning": (0, 255, 255),
"kerusakan_hama": (0, 0, 255),
"jamur_putih": (255, 0, 255),
"bercak_daun": (255, 255, 0)
}
# === KONEKSI SUPABASE ===
def initialize_supabase():
try:
supabase: Client = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
print("✅ Terhubung ke Supabase.")
return supabase
except Exception as e:
print("⚠️ Supabase gagal terhubung:", e)
return None
def delete_command(supabase, command_id):
supabase.table("commands").delete().eq("id", command_id).execute()
print(f"🗑️ Perintah ID {command_id} berhasil dihapus ✅")
def get_pending_command(supabase):
try:
resp = supabase.table("commands").select("*").order("created_at").limit(1).execute()
return resp.data[0] if resp.data else None
except Exception:
return None
# === CAMERA ===
def capture_image_from_webcam(index=1):
print(f"🎥 Membuka kamera (Index: {index})")
# Menggunakan CAP_V4L2 khusus untuk sistem Linux
cap = cv2.VideoCapture(index, cv2.CAP_DSHOW)
if not cap.isOpened():
print("❌ Kamera tidak ditemukan atau sedang digunakan.")
return None
# --- PENGATURAN HARDWARE MANUAL ---
# 1. Resolusi
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# 2. Fokus
cap.set(cv2.CAP_PROP_AUTOFOCUS, 1) # Auto focus tetap nyala
# 3. Exposure Manual
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 1)
# 4. White Balance Manual
cap.set(cv2.CAP_PROP_AUTO_WB, 3) # Auto WB
# --- PROSES PENGAMBILAN GAMBAR ---
print(" Stabilkan kamera (warm-up)...")
# Loop untuk membuang frame awal agar sensor stabil dengan pengaturan baru
for _ in range(30):
cap.read()
time.sleep(0.02)
ret, frame = cap.read()
cap.release()
if not ret or frame is None:
print("❌ Gagal menangkap gambar.")
return None
print("📸 Gambar berhasil diambil ✅")
return frame
# === YOLO MODEL ===
def load_yolo_model():
return YOLO(YOLO_MODEL_PATH)
def process_detection(model, image):
start = datetime.now()
results = model.predict(image, conf=0.5, verbose=False)
end = datetime.now()
objects = []
annotated = image.copy()
for box in results[0].boxes:
cid = int(box.cls[0])
cname = model.names.get(cid, "unknown")
acc = float(box.conf[0])
objects.append({"nama": cname, "akurasi": acc})
x1, y1, x2, y2 = map(int, box.xyxy[0])
color = COLOR_MAP.get(cname, (255,255,255))
cv2.rectangle(annotated, (x1,y1), (x2,y2), color, 2)
cv2.putText(annotated, f"{cname} ({acc*100:.1f}%)",
(x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
print(f"✅ Deteksi selesai {(end-start).total_seconds():.2f}s")
return objects, annotated, start, end
# === UPLOAD DATA ===
def upload_and_save_results(supabase, img, anno, objects, user_id, time_log):
ts = datetime.now()
fname = ts.strftime("%Y%m%d_%H%M%S")
_, b1 = cv2.imencode(".jpg", img)
_, b2 = cv2.imencode(".jpg", anno)
supabase.storage.from_(BUCKET_NAME).upload(f"original/{fname}.jpg", b1.tobytes())
supabase.storage.from_(BUCKET_NAME).upload(f"hasil_deteksi/{fname}.jpg", b2.tobytes())
url_orig = supabase.storage.from_(BUCKET_NAME).get_public_url(f"original/{fname}.jpg")
url_anno = supabase.storage.from_(BUCKET_NAME).get_public_url(f"hasil_deteksi/{fname}.jpg")
counts = Counter(o["nama"] for o in objects)
payload = {
"timestamp": ts.isoformat(),
"image_url": url_orig,
"url_hasil_deteksi": url_anno,
"accuracy": [round(o["akurasi"]*100,2) for o in objects],
"hama_deteksi": dict(counts),
"status": "selesai",
"user_id": user_id,
"log_waktu": time_log
}
supabase.table("gambar_hama").insert(payload).execute()
print("📤 Hasil deteksi disimpan ✅")
# === LOOP UTAMA ===
def main_loop():
supabase = initialize_supabase()
model = load_yolo_model()
while True:
try:
cmd = get_pending_command(supabase)
if cmd and cmd.get("action") == "capture":
uid = cmd["user_id"]
cid = cmd["id"]
print(f"\n🚀 Perintah dari user {uid}")
time_log = {
"ambil_perintah": datetime.now().isoformat()
}
frame = capture_image_from_webcam()
if frame is None: continue
objs, anno, t1, t2 = process_detection(model, frame)
time_log["mulai_deteksi"] = t1.isoformat()
time_log["selesai_deteksi"] = t2.isoformat()
time_log["mulai_upload"] = datetime.now().isoformat()
upload_and_save_results(supabase, frame, anno, objs, uid, time_log)
time_log["selesai_upload"] = datetime.now().isoformat()
delete_command(supabase, cid)
else:
print(".", end="", flush=True)
time.sleep(CHECK_INTERVAL)
except httpx.ConnectError:
print("\n🌐 Koneksi internet putus! Reconnect...")
supabase = initialize_supabase()
time.sleep(5)
except KeyboardInterrupt:
print("\n🛑 Sistem dihentikan oleh pengguna ✅")
break
except Exception as e:
print("\n⚠ ERROR:", e)
time.sleep(3)
if __name__ == "__main__":
main_loop()