demo_app.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. import streamlit as st
  2. import requests
  3. from ultralytics import YOLO
  4. import numpy as np
  5. from PIL import Image
  6. import io
  7. import base64
  8. import pandas as pd
  9. import plotly.express as px
  10. import plotly.graph_objects as go
  11. import json
  12. import os
  13. from datetime import datetime
  14. from fpdf import FPDF
  15. @st.dialog("📘 AI Interpretation Guide")
  16. def show_tech_guide():
  17. st.write("### 🧠 1. The 'Thinking' Phase: The Raw Tensor [1, 300, 6]")
  18. st.write("""
  19. When the AI 'thinks' about an image, it doesn't see 'Ripe' or 'Unripe'. It populates a
  20. fixed-size memory buffer (Tensor) with **300 potential candidates**. Each candidate is
  21. represented by a row of 6 numbers.
  22. """)
  23. st.table({
  24. "Tensor Index": ["0, 1, 2, 3", "4", "5"],
  25. "AI Output": ["Coordinates", "Confidence Score", "Class ID"],
  26. "Programmer's Logic": ["`[x1, y1, x2, y2]`", "`float (0.0 - 1.0)`", "`int (0-5)`"]
  27. })
  28. st.write("#### 🎯 The Coordinate Paradox (Pixels vs. Ratios)")
  29. st.write("""
  30. Depending on the engine, the **Values at Index 0-3** speak different languages.
  31. This is why the raw numbers won't match if you swap engines:
  32. """)
  33. col_a, col_b = st.columns(2)
  34. with col_a:
  35. st.info("**PyTorch Pathway (.pt)**")
  36. st.write("- **Format**: Absolute Pixels")
  37. st.write("- **Logic**: The AI outputs numbers mapped to the photo's resolution (e.g., `245.0`).")
  38. with col_b:
  39. st.success("**ONNX Pathway (.onnx)**")
  40. st.write("- **Format**: Normalized Ratios")
  41. st.write("- **Logic**: The AI outputs percentages (0.0 to 1.0) relative to its internal 640x640 grid (e.g., `0.38`).")
  42. st.write("---")
  43. st.write("### 🎯 2. What is 'Confidence'? (The Probability Filter)")
  44. st.write("""
  45. Confidence is the AI's **mathematical certainty** that an object exists in a specific box.
  46. It is the product of *Objectness* (Is something there?) and *Class Probability* (What is it?).
  47. """)
  48. st.table({
  49. "Confidence Value": ["> 0.90", "0.50 - 0.89", "< 0.25 (Threshold)"],
  50. "Interpretation": ["**Certain**: Clear, unobstructed view.", "**Likely**: Valid, but possibly obscured by fronds.", "**Noise**: Discarded to prevent False Positives."]
  51. })
  52. st.write("---")
  53. st.write("### 🛠️ 3. The Custom Handler (The Translation Layer)")
  54. st.write("""
  55. Because ONNX returns raw ratios, we built a **Manual Scaling Handler**. It maps those
  56. `0.0 - 1.0` values back to your high-resolution photo pixels.
  57. This explains our two key metrics:
  58. - **Inference Speed**: The time the AI spent populating the Raw Tensor.
  59. - **Post-Processing**: The time our code spent 'translating' that Tensor into labels and pixels.
  60. """)
  61. st.write("---")
  62. st.markdown("""
  63. Your detection environment is powered by **YOLO26**, a custom architectural fork designed for zero-latency industrial sorting.
  64. ### ⚡ Performance Comparison
  65. | Feature | YOLO26 (ONNX) | YOLO26 (Native) |
  66. | :--- | :--- | :--- |
  67. | **Coordinate System** | Normalized (0.0 - 1.0) | Absolute (Pixels) |
  68. | **Primary Use Case** | Real-time Edge Sorting | High-Resolution Auditing |
  69. | **Post-Processing** | None (NMS-Free) | Standard NMS |
  70. """)
  71. # --- 1. Global Backend Check ---
  72. API_BASE_URL = "http://localhost:8000"
  73. # MPOB Color Map for Overlays (Global for consistency)
  74. overlay_colors = {
  75. 'Ripe': '#22c55e', # Industrial Green
  76. 'Underripe': '#fbbf24', # Industrial Orange
  77. 'Unripe': '#3b82f6', # Industrial Blue
  78. 'Abnormal': '#dc2626', # Critical Red
  79. 'Empty_Bunch': '#64748b',# Waste Gray
  80. 'Overripe': '#7c2d12' # Dark Brown/Orange
  81. }
  82. # Helper to reset results when files change or engine switches
  83. def get_color(class_name):
  84. """Robust color lookup for consistent across models."""
  85. # Normalize: "Under-ripe" -> "underripe", "Empty Bunch" -> "emptybunch"
  86. norm_name = class_name.lower().replace("-", "").replace("_", "").replace(" ", "")
  87. # Map normalized names to your MPOB standard colors
  88. color_map = {k.lower().replace("_", ""): v for k, v in overlay_colors.items()}
  89. if norm_name in color_map:
  90. return color_map[norm_name]
  91. # Fallback: Generate a consistent unique color for benchmark-only classes
  92. import hashlib
  93. return f"#{hashlib.md5(class_name.encode()).hexdigest()[:6]}"
  94. def reset_single_results():
  95. st.session_state.last_detection = None
  96. def reset_batch_results():
  97. st.session_state.last_batch_results = None
  98. def reset_all_analysis():
  99. """Global reset for all active analysis views."""
  100. st.session_state.last_detection = None
  101. st.session_state.last_batch_results = None
  102. # Increment uploader keys to 'forget' current files (Clear Canvas)
  103. if "single_uploader_key" not in st.session_state:
  104. st.session_state.single_uploader_key = 0
  105. st.session_state.single_uploader_key += 1
  106. if "batch_uploader_key" not in st.session_state:
  107. st.session_state.batch_uploader_key = 0
  108. st.session_state.batch_uploader_key += 1
  109. def check_backend():
  110. try:
  111. res = requests.get(f"{API_BASE_URL}/get_confidence", timeout=2)
  112. return res.status_code == 200
  113. except:
  114. return False
  115. backend_active = check_backend()
  116. # LOCAL MODEL LOADING REMOVED (YOLO26 Clean Sweep)
  117. # UI now relies entirely on Backend API for NMS-Free inference.
  118. if not backend_active:
  119. st.error("⚠️ Backend API is offline!")
  120. st.info("Please start the backend server first (e.g., `python main.py`) to unlock AI features.")
  121. if st.button("🔄 Retry Connection"):
  122. st.rerun()
  123. st.stop() # Stops execution here, effectively disabling the app
  124. # --- 2. Main Page Config (Only rendered if backend is active) ---
  125. st.set_page_config(page_title="Palm Oil Ripeness AI (YOLO26)", layout="wide")
  126. st.title("🌴 Palm Oil FFB Management System")
  127. st.markdown("### Production-Ready AI Analysis & Archival")
  128. # --- Sidebar ---
  129. st.sidebar.header("Backend Controls")
  130. def update_confidence():
  131. new_conf = st.session_state.conf_slider
  132. try:
  133. requests.post(f"{API_BASE_URL}/set_confidence", json={"threshold": new_conf})
  134. st.toast(f"Threshold updated to {new_conf}")
  135. except:
  136. st.sidebar.error("Failed to update threshold")
  137. # We already know backend is up here
  138. response = requests.get(f"{API_BASE_URL}/get_confidence")
  139. current_conf = response.json().get("current_confidence", 0.25)
  140. st.sidebar.success(f"Connected to API")
  141. st.sidebar.info("Engine: YOLO26 NMS-Free (Inference: ~39ms)")
  142. # Synchronized Slider
  143. st.sidebar.slider(
  144. "Confidence Threshold",
  145. 0.1, 1.0,
  146. value=float(current_conf),
  147. key="conf_slider",
  148. on_change=update_confidence
  149. )
  150. st.sidebar.markdown("---")
  151. # Inference Engine
  152. engine_choice = st.sidebar.selectbox(
  153. "Select Model Engine:",
  154. ["YOLO26 (ONNX - High Speed)", "YOLO26 (PyTorch - Native)", "Sawit-TBS (Benchmark)"],
  155. index=0,
  156. on_change=reset_all_analysis # Clear canvas on engine switch
  157. )
  158. # Map selection to internal labels
  159. engine_map = {
  160. "YOLO26 (ONNX - High Speed)": "onnx",
  161. "YOLO26 (PyTorch - Native)": "pytorch",
  162. "Sawit-TBS (Benchmark)": "benchmark"
  163. }
  164. st.sidebar.markdown("---")
  165. model_type = engine_map[engine_choice]
  166. if st.sidebar.button("❓ How to read results?", icon="📘", width='stretch'):
  167. show_tech_guide()
  168. # Function definitions moved to top
  169. def display_interactive_results(image, detections, key=None):
  170. """Renders image with interactive hover-boxes using Plotly."""
  171. img_width, img_height = image.size
  172. fig = go.Figure()
  173. # Add the palm image as the background
  174. fig.add_layout_image(
  175. dict(source=image, x=0, y=img_height, sizex=img_width, sizey=img_height,
  176. sizing="stretch", opacity=1, layer="below", xref="x", yref="y")
  177. )
  178. # Configure axes to match image dimensions
  179. fig.update_xaxes(showgrid=False, range=(0, img_width), zeroline=False, visible=False)
  180. fig.update_yaxes(showgrid=False, range=(0, img_height), zeroline=False, visible=False, scaleanchor="x")
  181. # Add interactive boxes
  182. for i, det in enumerate(detections):
  183. x1, y1, x2, y2 = det['box']
  184. # Plotly y-axis is inverted relative to PIL, so we flip y
  185. y_top, y_bottom = img_height - y1, img_height - y2
  186. color = get_color(det['class'])
  187. is_bench = (st.session_state.get('engine_choice') == "Sawit-TBS (Benchmark)")
  188. # The 'Hover' shape
  189. bunch_id = det.get('bunch_id', i+1)
  190. fig.add_trace(go.Scatter(
  191. x=[x1, x2, x2, x1, x1],
  192. y=[y_top, y_top, y_bottom, y_bottom, y_top],
  193. fill="toself",
  194. fillcolor=color,
  195. opacity=0.5 if is_bench else 0.3, # Stronger highlight for benchmark
  196. mode='lines',
  197. line=dict(color=color, width=5 if is_bench else 3, dash='dot' if is_bench else 'solid'),
  198. name=f"ID: #{bunch_id}", # Unified ID Tag
  199. text=f"<b>ID: #{bunch_id}</b><br>Grade: {det['class']}<br>Score: {det['confidence']:.2f}<br>Alert: {det['is_health_alert']}",
  200. hoverinfo="text"
  201. ))
  202. fig.update_layout(width=800, height=600, margin=dict(l=0, r=0, b=0, t=0), showlegend=False)
  203. st.plotly_chart(fig, width='stretch', key=key)
  204. def annotate_image(image, detections):
  205. """Draws high-visibility 'Plated Labels' and boxes on the image."""
  206. from PIL import ImageDraw, ImageFont
  207. draw = ImageDraw.Draw(image)
  208. # 1. Dynamic Font Scaling (width // 40 as requested)
  209. font_size = max(20, image.width // 40)
  210. try:
  211. # standard Windows font paths for agent environment
  212. font_path = "C:\\Windows\\Fonts\\arialbd.ttf" # Bold for higher visibility
  213. if not os.path.exists(font_path):
  214. font_path = "C:\\Windows\\Fonts\\arial.ttf"
  215. if os.path.exists(font_path):
  216. font = ImageFont.truetype(font_path, font_size)
  217. else:
  218. font = ImageFont.load_default()
  219. except:
  220. font = ImageFont.load_default()
  221. for det in detections:
  222. box = det['box'] # [x1, y1, x2, y2]
  223. cls = det['class']
  224. conf = det['confidence']
  225. bunch_id = det.get('bunch_id', '?')
  226. color = get_color(cls)
  227. is_bench = (st.session_state.get('engine_choice') == "Sawit-TBS (Benchmark)")
  228. # 2. Draw Heavy-Duty Bounding Box
  229. line_width = max(6 if is_bench else 4, image.width // (80 if is_bench else 150))
  230. draw.rectangle(box, outline=color, width=line_width)
  231. # 3. Draw 'Plated Label' (Background Shaded)
  232. label = f"#{bunch_id} {cls} {conf:.2f}"
  233. try:
  234. # Precise background calculation using textbbox
  235. l, t, r, b = draw.textbbox((box[0], box[1]), label, font=font)
  236. # Shift background up so it doesn't obscure the fruit
  237. bg_rect = [l - 2, t - (b - t) - 10, r + 2, t - 6]
  238. draw.rectangle(bg_rect, fill=color)
  239. # Draw text inside the plate
  240. draw.text((l, t - (b - t) - 8), label, fill="white", font=font)
  241. except:
  242. # Simple fallback
  243. draw.text((box[0], box[1] - font_size), label, fill=color)
  244. return image
  245. def generate_batch_report(data, uploaded_files_map=None):
  246. """Generates a professional PDF report for batch results with visual evidence."""
  247. from PIL import ImageDraw
  248. pdf = FPDF()
  249. pdf.add_page()
  250. pdf.set_font("Arial", "B", 16)
  251. pdf.cell(190, 10, "Palm Oil FFB Harvest Quality Report", ln=True, align="C")
  252. pdf.set_font("Arial", "", 12)
  253. pdf.cell(190, 10, f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align="C")
  254. pdf.ln(10)
  255. # 1. Summary Table
  256. pdf.set_font("Arial", "B", 14)
  257. pdf.cell(190, 10, "1. Batch Summary", ln=True)
  258. pdf.set_font("Arial", "", 12)
  259. summary = data.get('industrial_summary', {})
  260. total_bunches = data.get('total_count', 0)
  261. pdf.cell(95, 10, "Metric", border=1)
  262. pdf.cell(95, 10, "Value", border=1, ln=True)
  263. pdf.cell(95, 10, "Total Bunches Detected", border=1)
  264. pdf.cell(95, 10, str(total_bunches), border=1, ln=True)
  265. for grade, count in summary.items():
  266. if count > 0:
  267. pdf.cell(95, 10, f"Grade: {grade}", border=1)
  268. pdf.cell(95, 10, str(count), border=1, ln=True)
  269. pdf.ln(10)
  270. # 2. Strategic Insights
  271. pdf.set_font("Arial", "B", 14)
  272. pdf.cell(190, 10, "2. Strategic Yield Insights", ln=True)
  273. pdf.set_font("Arial", "", 12)
  274. unripe = summary.get('Unripe', 0)
  275. underripe = summary.get('Underripe', 0)
  276. loss = unripe + underripe
  277. if loss > 0:
  278. pdf.multi_cell(190, 10, f"WARNING: {loss} bunches were harvested before peak ripeness. "
  279. "This directly impacts the Oil Extraction Rate (OER) and results in potential yield loss.")
  280. else:
  281. pdf.multi_cell(190, 10, "EXCELLENT: All detected bunches meet prime ripeness standards. Harvest efficiency is 100%.")
  282. # Critical Alerts
  283. abnormal = summary.get('Abnormal', 0)
  284. empty = summary.get('Empty_Bunch', 0)
  285. if abnormal > 0 or empty > 0:
  286. pdf.ln(5)
  287. pdf.set_text_color(220, 0, 0)
  288. pdf.set_font("Arial", "B", 12)
  289. pdf.cell(190, 10, "CRITICAL HEALTH ALERTS:", ln=True)
  290. pdf.set_font("Arial", "", 12)
  291. if abnormal > 0:
  292. pdf.cell(190, 10, f"- {abnormal} Abnormal Bunches detected (Requires immediate field inspection).", ln=True)
  293. if empty > 0:
  294. pdf.cell(190, 10, f"- {empty} Empty Bunches detected (Waste reduction needed).", ln=True)
  295. pdf.set_text_color(0, 0, 0)
  296. # 3. Visual Evidence Section
  297. if 'detailed_results' in data and uploaded_files_map:
  298. pdf.add_page()
  299. pdf.set_font("Arial", "B", 14)
  300. pdf.cell(190, 10, "3. Visual Batch Evidence (AI Overlay)", ln=True)
  301. pdf.ln(5)
  302. # Group detections by filename
  303. results_by_file = {}
  304. for res in data['detailed_results']:
  305. fname = res['filename']
  306. if fname not in results_by_file:
  307. results_by_file[fname] = []
  308. results_by_file[fname].append(res['detection'])
  309. for fname, detections in results_by_file.items():
  310. if fname in uploaded_files_map:
  311. img_bytes = uploaded_files_map[fname]
  312. img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
  313. draw = ImageDraw.Draw(img)
  314. # Drawing annotated boxes for PDF using high-visibility utility
  315. annotate_image(img, detections)
  316. # Save to temp file for PDF
  317. temp_img_path = f"temp_report_{fname}"
  318. img.save(temp_img_path)
  319. # Check if we need a new page based on image height (rough estimate)
  320. if pdf.get_y() > 200:
  321. pdf.add_page()
  322. pdf.image(temp_img_path, x=10, w=150)
  323. pdf.set_font("Arial", "I", 10)
  324. pdf.cell(190, 10, f"Annotated: {fname}", ln=True)
  325. pdf.ln(5)
  326. os.remove(temp_img_path)
  327. # Footer
  328. pdf.set_y(-15)
  329. pdf.set_font("Arial", "I", 8)
  330. pdf.cell(190, 10, "Generated by Palm Oil AI Desktop PoC - YOLO26 Engine", align="C")
  331. return pdf.output(dest='S')
  332. # --- Tabs ---
  333. tab1, tab2, tab3, tab4 = st.tabs(["Single Analysis", "Batch Processing", "Similarity Search", "History Vault"])
  334. # --- Tab 1: Single Analysis ---
  335. with tab1:
  336. st.subheader("Analyze Single Bunch")
  337. # 1. Initialize Uploader Key
  338. if "single_uploader_key" not in st.session_state:
  339. st.session_state.single_uploader_key = 0
  340. uploaded_file = st.file_uploader(
  341. "Upload a bunch image...",
  342. type=["jpg", "jpeg", "png"],
  343. key=f"single_{st.session_state.single_uploader_key}",
  344. on_change=reset_single_results
  345. )
  346. if uploaded_file:
  347. # State initialization
  348. if "last_detection" not in st.session_state:
  349. st.session_state.last_detection = None
  350. # 1. Auto-Detection Trigger
  351. if uploaded_file and st.session_state.last_detection is None:
  352. with st.spinner(f"Processing with {model_type.upper()} Engine..."):
  353. files = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
  354. payload = {"model_type": model_type}
  355. res = requests.post(f"{API_BASE_URL}/analyze", files=files, data=payload)
  356. if res.status_code == 200:
  357. st.session_state.last_detection = res.json()
  358. st.rerun() # Refresh to show results immediately
  359. else:
  360. st.error(f"Detection Failed: {res.text}")
  361. # 2. Results Layout
  362. if st.session_state.last_detection:
  363. # Redo Button at the top for easy access
  364. if st.button("🔄 Re-analyze Image", width='stretch', type="primary", help="Force a fresh detection (useful if threshold changed)."):
  365. st.session_state.last_detection = None
  366. st.rerun()
  367. data = st.session_state.last_detection
  368. st.divider()
  369. if model_type == "benchmark":
  370. st.info("💡 **Benchmark Mode**: Labels and colors are determined by the external model's architecture. Some labels may not match standard MPOB categories.")
  371. st.write("### 📈 Manager's Dashboard")
  372. m_col1, m_col2, m_col3, m_col4 = st.columns(4)
  373. with m_col1:
  374. st.metric("Total Bunches", data.get('total_count', 0))
  375. with m_col2:
  376. if model_type == "benchmark":
  377. # For benchmark model, show the top detected class instead of 'Healthy'
  378. top_class = "None"
  379. if data.get('industrial_summary'):
  380. top_class = max(data['industrial_summary'], key=data['industrial_summary'].get)
  381. st.metric("Top Detected Class", top_class)
  382. else:
  383. st.metric("Healthy (Ripe)", data['industrial_summary'].get('Ripe', 0))
  384. with m_col3:
  385. # Refined speed label based on engine
  386. speed_label = "Raw Speed (Unlabeled)" if model_type == "onnx" else "Wrapped Speed (Auto-Labeled)"
  387. st.metric("Inference Speed", f"{data.get('inference_ms', 0):.1f} ms", help=speed_label)
  388. with m_col4:
  389. st.metric("Post-Processing", f"{data.get('processing_ms', 0):.1f} ms", help="Labeling/Scaling overhead")
  390. st.divider()
  391. # Side-by-Side View (Technical Trace)
  392. img = Image.open(uploaded_file).convert("RGB")
  393. if st.session_state.get('tech_trace', False):
  394. t_col1, t_col2 = st.columns(2)
  395. with t_col1:
  396. st.subheader("🔢 Raw Output Tensor (The Math)")
  397. st.caption("First 5 rows of the 1x300x6 detection tensor.")
  398. st.json(data.get('raw_array_sample', []))
  399. with t_col2:
  400. st.subheader("🎨 AI Interpretation")
  401. img_annotated = annotate_image(img.copy(), data['detections'])
  402. st.image(img_annotated, width='stretch')
  403. else:
  404. # Regular View
  405. st.write("### 🔍 AI Analytical View")
  406. display_interactive_results(img, data['detections'], key="main_viewer")
  407. col1, col2 = st.columns([1.5, 1]) # Keep original col structure for summary below
  408. with col1:
  409. col_tech_h1, col_tech_h2 = st.columns([1, 1])
  410. with col_tech_h1:
  411. st.write("#### 🛠️ Technical Evidence")
  412. with col_tech_h2:
  413. st.session_state.tech_trace = st.toggle("🔬 Side-by-Side Trace", value=st.session_state.get('tech_trace', False))
  414. with st.expander("Raw Output Tensor (NMS-Free)", expanded=False):
  415. coord_type = "Absolute Pixels" if model_type == "pytorch" else "Normalized Ratios (0.0-1.0)"
  416. st.warning(f"Engine detected: {model_type.upper()} | Coordinate System: {coord_type}")
  417. st.json(data.get('raw_array_sample', []))
  418. with st.container(border=True):
  419. st.write("### 🏷️ Detection Results")
  420. if not data['detections']:
  421. st.warning("No Fresh Fruit Bunches detected.")
  422. else:
  423. for det in data['detections']:
  424. st.info(f"### Bunch #{det['bunch_id']}: {det['class']} ({det['confidence']:.2%})")
  425. st.write("### 📊 Harvest Quality Mix")
  426. # Convert industrial_summary dictionary to a DataFrame for charting
  427. summary_df = pd.DataFrame(
  428. list(data['industrial_summary'].items()),
  429. columns=['Grade', 'Count']
  430. )
  431. # Filter out classes with 0 count for a cleaner chart
  432. summary_df = summary_df[summary_df['Count'] > 0]
  433. if not summary_df.empty:
  434. # Create a Pie Chart to show the proportion of each grade
  435. fig = px.pie(summary_df, values='Count', names='Grade',
  436. color='Grade',
  437. color_discrete_map={
  438. 'Ripe': '#22c55e', # Industrial Green
  439. 'Underripe': '#fbbf24', # Industrial Orange
  440. 'Unripe': '#3b82f6', # Industrial Blue
  441. 'Abnormal': '#dc2626', # Critical Red
  442. 'Empty_Bunch': '#64748b' # Waste Gray
  443. },
  444. hole=0.4)
  445. fig.update_layout(margin=dict(t=0, b=0, l=0, r=0), height=300)
  446. st.plotly_chart(fig, width='stretch', key="single_pie")
  447. # 💡 Strategic R&D Insight: Harvest Efficiency
  448. st.write("---")
  449. st.write("#### 💡 Strategic R&D Insight")
  450. unripe_count = data['industrial_summary'].get('Unripe', 0)
  451. underripe_count = data['industrial_summary'].get('Underripe', 0)
  452. total_non_prime = unripe_count + underripe_count
  453. st.write(f"🌑 **Unripe (Mentah):** {unripe_count}")
  454. st.write(f"🌗 **Underripe (Kurang Masak):** {underripe_count}")
  455. if total_non_prime > 0:
  456. st.warning(f"🚨 **Potential Yield Loss:** {total_non_prime} bunches harvested too early. This will reduce OER (Oil Extraction Rate).")
  457. else:
  458. st.success("✅ **Harvest Efficiency:** 100% Prime Ripeness detected.")
  459. # High-Priority Health Alert
  460. if data['industrial_summary'].get('Abnormal', 0) > 0:
  461. st.error(f"🚨 CRITICAL: {data['industrial_summary']['Abnormal']} Abnormal Bunches Detected!")
  462. if data['industrial_summary'].get('Empty_Bunch', 0) > 0:
  463. st.warning(f"⚠️ ALERT: {data['industrial_summary']['Empty_Bunch']} Empty Bunches Detected.")
  464. # 3. Cloud Actions (Only if detections found)
  465. st.write("---")
  466. st.write("#### ✨ Cloud Archive")
  467. if st.button("🚀 Save to Atlas (Vectorize)", width='stretch'):
  468. with st.spinner("Archiving..."):
  469. import json
  470. primary_det = data['detections'][0]
  471. payload = {"detection_data": json.dumps(primary_det)}
  472. files_cloud = {"file": (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
  473. res_cloud = requests.post(f"{API_BASE_URL}/vectorize_and_store", files=files_cloud, data=payload)
  474. if res_cloud.status_code == 200:
  475. res_json = res_cloud.json()
  476. if res_json["status"] == "success":
  477. st.success(f"Archived! ID: `{res_json['record_id'][:8]}...`")
  478. else:
  479. st.error(f"Cloud Error: {res_json['message']}")
  480. else:
  481. st.error("Failed to connect to cloud service")
  482. if st.button("🚩 Flag Misclassification", width='stretch', type="secondary"):
  483. # Save to local feedback folder
  484. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  485. feedback_id = f"fb_{timestamp}"
  486. img_path = f"feedback/{feedback_id}.jpg"
  487. json_path = f"feedback/{feedback_id}.json"
  488. # Save image
  489. Image.open(uploaded_file).save(img_path)
  490. # Save metadata
  491. feedback_data = {
  492. "original_filename": uploaded_file.name,
  493. "timestamp": timestamp,
  494. "detections": data['detections'],
  495. "threshold_used": data['current_threshold']
  496. }
  497. with open(json_path, "w") as f:
  498. json.dump(feedback_data, f, indent=4)
  499. st.toast("✅ Feedback saved to local vault!", icon="🚩")
  500. if st.button("💾 Local History Vault (Auto-Saved)", width='stretch', type="secondary", disabled=True):
  501. pass
  502. st.caption("✅ This analysis was automatically archived to the local vault.")
  503. # --- Tab 2: Batch Processing ---
  504. with tab2:
  505. st.subheader("Bulk Analysis")
  506. # 1. Initialize Session State
  507. if "batch_uploader_key" not in st.session_state:
  508. st.session_state.batch_uploader_key = 0
  509. if "last_batch_results" not in st.session_state:
  510. st.session_state.last_batch_results = None
  511. # 2. Display Persisted Results (if any)
  512. if st.session_state.last_batch_results:
  513. res_data = st.session_state.last_batch_results
  514. with st.container(border=True):
  515. st.success(f"✅ Successfully processed {res_data['processed_count']} images.")
  516. # Batch Summary Dashboard
  517. st.write("### 📈 Batch Quality Overview")
  518. batch_summary = res_data.get('industrial_summary', {})
  519. if batch_summary:
  520. sum_df = pd.DataFrame(list(batch_summary.items()), columns=['Grade', 'Count'])
  521. sum_df = sum_df[sum_df['Count'] > 0]
  522. b_col1, b_col2 = st.columns([1, 1])
  523. with b_col1:
  524. st.dataframe(sum_df, hide_index=True, width='stretch')
  525. with b_col2:
  526. if not sum_df.empty:
  527. fig_batch = px.bar(sum_df, x='Grade', y='Count', color='Grade',
  528. color_discrete_map={
  529. 'Ripe': '#22c55e',
  530. 'Underripe': '#fbbf24',
  531. 'Unripe': '#3b82f6',
  532. 'Abnormal': '#dc2626',
  533. 'Empty_Bunch': '#64748b'
  534. })
  535. fig_batch.update_layout(margin=dict(t=0, b=0, l=0, r=0), height=200, showlegend=False)
  536. st.plotly_chart(fig_batch, width='stretch', key="batch_bar")
  537. if batch_summary.get('Abnormal', 0) > 0:
  538. st.error(f"🚨 BATCH CRITICAL: {batch_summary['Abnormal']} Abnormal Bunches found in this batch!")
  539. st.write("Generated Record IDs:")
  540. st.code(res_data['record_ids'])
  541. # --- 4. Batch Evidence Gallery ---
  542. st.write("### 🖼️ Detailed Detection Evidence")
  543. if 'detailed_results' in res_data:
  544. # Group results by filename for gallery
  545. gallery_map = {}
  546. for res in res_data['detailed_results']:
  547. fname = res['filename']
  548. if fname not in gallery_map:
  549. gallery_map[fname] = []
  550. gallery_map[fname].append(res['detection'])
  551. # Show images with overlays using consistent utility
  552. for up_file in uploaded_files:
  553. if up_file.name in gallery_map:
  554. with st.container(border=True):
  555. g_img = Image.open(up_file).convert("RGB")
  556. g_annotated = annotate_image(g_img, gallery_map[up_file.name])
  557. st.image(g_annotated, caption=f"Evidence: {up_file.name}", width='stretch')
  558. # PDF Export Button (Pass images map)
  559. files_map = {f.name: f.getvalue() for f in uploaded_files}
  560. pdf_bytes = generate_batch_report(res_data, files_map)
  561. st.download_button(
  562. label="📄 Download Executive Batch Report (PDF)",
  563. data=pdf_bytes,
  564. file_name=f"PalmOil_BatchReport_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf",
  565. mime="application/pdf",
  566. width='stretch'
  567. )
  568. if st.button("Clear Results & Start New Batch", width='stretch'):
  569. st.session_state.last_batch_results = None
  570. st.rerun()
  571. st.divider()
  572. # 3. Uploader UI
  573. col_batch1, col_batch2 = st.columns([4, 1])
  574. with col_batch1:
  575. uploaded_files = st.file_uploader(
  576. "Upload multiple images...",
  577. type=["jpg", "jpeg", "png"],
  578. accept_multiple_files=True,
  579. key=f"batch_{st.session_state.batch_uploader_key}",
  580. on_change=reset_batch_results
  581. )
  582. with col_batch2:
  583. st.write("##") # Alignment
  584. if st.session_state.last_batch_results is None and uploaded_files:
  585. if st.button("🔍 Process Batch", type="primary", width='stretch'):
  586. with st.spinner(f"Analyzing {len(uploaded_files)} images with {model_type.upper()}..."):
  587. files = [("files", (f.name, f.getvalue(), f.type)) for f in uploaded_files]
  588. payload = {"model_type": model_type}
  589. res = requests.post(f"{API_BASE_URL}/process_batch", files=files, data=payload)
  590. if res.status_code == 200:
  591. data = res.json()
  592. if data["status"] == "success":
  593. st.session_state.last_batch_results = data
  594. st.session_state.batch_uploader_key += 1
  595. st.rerun()
  596. elif data["status"] == "partial_success":
  597. st.warning(data["message"])
  598. st.info(f"Successfully detected {data['detections_count']} bunches locally.")
  599. else:
  600. st.error(f"Batch Error: {data['message']}")
  601. else:
  602. st.error(f"Batch Processing Failed: {res.text}")
  603. if st.button("🗑️ Reset Uploader"):
  604. st.session_state.batch_uploader_key += 1
  605. st.session_state.last_batch_results = None
  606. st.rerun()
  607. # --- Tab 3: Similarity Search ---
  608. with tab3:
  609. st.subheader("Hybrid Semantic Search")
  610. st.markdown("Search records by either **Image Similarity** or **Natural Language Query**.")
  611. with st.form("hybrid_search_form"):
  612. col_input1, col_input2 = st.columns(2)
  613. with col_input1:
  614. search_file = st.file_uploader("Option A: Search Image...", type=["jpg", "jpeg", "png"], key="search")
  615. with col_input2:
  616. text_query = st.text_input("Option B: Natural Language Query", placeholder="e.g., 'ripe bunches with dark spots' or 'unripe fruit'")
  617. top_k = st.slider("Results Limit (Top K)", 1, 20, 3)
  618. submit_search = st.form_submit_button("Run Semantic Search")
  619. if submit_search:
  620. if not search_file and not text_query:
  621. st.warning("Please provide either an image or a text query.")
  622. else:
  623. with st.spinner("Searching Vector Index..."):
  624. payload = {"limit": top_k}
  625. # If an image is uploaded, it takes precedence for visual search
  626. if search_file:
  627. files = {"file": (search_file.name, search_file.getvalue(), search_file.type)}
  628. # Pass top_k as part of the data
  629. res = requests.post(f"{API_BASE_URL}/search_hybrid", files=files, data=payload)
  630. # Otherwise, use text query
  631. elif text_query:
  632. payload["text_query"] = text_query
  633. # Send as form-data (data=) to match FastAPI's Form(None)
  634. res = requests.post(f"{API_BASE_URL}/search_hybrid", data=payload)
  635. if res.status_code == 200:
  636. results = res.json().get("results", [])
  637. if not results:
  638. st.warning("No similar records found.")
  639. else:
  640. st.success(f"Found {len(results)} matches.")
  641. for item in results:
  642. with st.container(border=True):
  643. c1, c2 = st.columns([1, 2])
  644. # Fetch the image for this result
  645. rec_id = item["_id"]
  646. img_res = requests.get(f"{API_BASE_URL}/get_image/{rec_id}")
  647. with c1:
  648. if img_res.status_code == 200:
  649. img_b64 = img_res.json().get("image_data")
  650. if img_b64:
  651. st.image(base64.b64decode(img_b64), width=250)
  652. else:
  653. st.write("No image data found.")
  654. else:
  655. st.write("Failed to load image.")
  656. with c2:
  657. st.write(f"**Class:** {item['ripeness_class']}")
  658. st.write(f"**Similarity Score:** {item['score']:.4f}")
  659. st.write(f"**Timestamp:** {item['timestamp']}")
  660. st.write(f"**ID:** `{rec_id}`")
  661. else:
  662. st.error(f"Search failed: {res.text}")
  663. # --- Tab 4: History Vault ---
  664. with tab4:
  665. st.subheader("📜 Local History Vault")
  666. st.caption("Industrial-grade audit log of all past AI harvest scans.")
  667. if "selected_history_id" not in st.session_state:
  668. st.session_state.selected_history_id = None
  669. try:
  670. res = requests.get(f"{API_BASE_URL}/get_history")
  671. if res.status_code == 200:
  672. history_data = res.json().get("history", [])
  673. if not history_data:
  674. st.info("No saved records found in the vault.")
  675. else:
  676. if st.session_state.selected_history_id is None:
  677. # --- 1. ListView Mode (Management Dashboard) ---
  678. st.write("### 📋 Audit Log")
  679. # Prepare searchable dataframe
  680. df_history = pd.DataFrame(history_data)
  681. # Clean up for display
  682. display_df = df_history[['id', 'timestamp', 'engine', 'filename', 'inference_ms']].copy()
  683. display_df.columns = ['ID', 'Date/Time', 'Engine', 'Filename', 'Inference (ms)']
  684. st.dataframe(
  685. display_df,
  686. hide_index=True,
  687. width='stretch',
  688. column_config={
  689. "ID": st.column_config.NumberColumn(width="small"),
  690. "Inference (ms)": st.column_config.NumberColumn(format="%.1f ms")
  691. }
  692. )
  693. # Industrial Selection UI
  694. hist_col1, hist_col2 = st.columns([3, 1])
  695. with hist_col1:
  696. target_id = st.selectbox(
  697. "Select Record for Deep Dive Analysis",
  698. options=df_history['id'].tolist(),
  699. format_func=lambda x: f"Record #{x} - {df_history[df_history['id']==x]['filename'].values[0]}"
  700. )
  701. with hist_col2:
  702. st.write("##") # Alignment
  703. if st.button("🔬 Start Deep Dive", type="primary", width='stretch'):
  704. st.session_state.selected_history_id = target_id
  705. st.rerun()
  706. else:
  707. # --- 2. Detail View Mode (Technical Auditor) ---
  708. record = next((item for item in history_data if item["id"] == st.session_state.selected_history_id), None)
  709. if not record:
  710. st.error("Audit record not found.")
  711. if st.button("Back to List"):
  712. st.session_state.selected_history_id = None
  713. st.rerun()
  714. else:
  715. st.button("⬅️ Back to Audit Log", on_click=lambda: st.session_state.update({"selected_history_id": None}))
  716. st.divider()
  717. st.write(f"## 🔍 Deep Dive: Record #{record['id']}")
  718. engine_val = record.get('engine', 'Unknown')
  719. st.caption(f"Original Filename: `{record['filename']}` | Processed: `{record['timestamp']}` | Engine: `{engine_val.upper()}`")
  720. detections = json.loads(record['detections'])
  721. summary = json.loads(record['summary'])
  722. # Metrics Executive Summary
  723. h_col1, h_col2, h_col3, h_col4 = st.columns(4)
  724. with h_col1:
  725. st.metric("Total Bunches", sum(summary.values()))
  726. with h_col2:
  727. st.metric("Healthy (Ripe)", summary.get('Ripe', 0))
  728. with h_col3:
  729. st.metric("Engine Performance", f"{record.get('inference_ms', 0) or 0:.1f} ms")
  730. with h_col4:
  731. st.metric("Labeling Overhead", f"{record.get('processing_ms', 0) or 0:.1f} ms")
  732. # Re-Annotate Archived Image
  733. if os.path.exists(record['archive_path']):
  734. with open(record['archive_path'], "rb") as f:
  735. hist_img = Image.open(f).convert("RGB")
  736. # Side-by-Side: Interactive vs Static Plate
  737. v_tab1, v_tab2 = st.tabs(["Interactive Plotly View", "Static Annotated Evidence"])
  738. with v_tab1:
  739. display_interactive_results(hist_img, detections, key=f"hist_plotly_{record['id']}")
  740. with v_tab2:
  741. img_plate = annotate_image(hist_img.copy(), detections)
  742. st.image(img_plate, width='stretch', caption="Point-of-Harvest AI Interpretation")
  743. else:
  744. st.warning(f"Technical Error: Archive file missing at `{record['archive_path']}`")
  745. # Technical Evidence Expander (Mathematical Audit)
  746. st.divider()
  747. st.write("### 🛠️ Technical Audit Trail")
  748. with st.expander("🔬 View Raw Mathematical Tensor", expanded=False):
  749. st.info("This is the exact numerical output from the AI engine prior to human-readable transformation.")
  750. raw_data = record.get('raw_tensor')
  751. if raw_data:
  752. try:
  753. st.json(json.loads(raw_data))
  754. except:
  755. st.code(raw_data)
  756. else:
  757. st.warning("No raw tensor trace was archived for this legacy record.")
  758. else:
  759. st.error(f"Vault Connection Failed: {res.text}")
  760. except Exception as e:
  761. st.error(f"Audit System Error: {str(e)}")