SURESHBEEKHANI commited on
Commit
d44ba7f
Β·
verified Β·
1 Parent(s): c613989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -53
app.py CHANGED
@@ -4,6 +4,7 @@ import cv2
4
  import os
5
  import base64
6
  import io
 
7
  from dotenv import load_dotenv
8
  from groq import Groq
9
  from reportlab.lib.pagesizes import letter
@@ -81,50 +82,6 @@ def process_image_data(uploaded_file):
81
  st.error(f"Image processing error: {str(e)}")
82
  return None, None
83
 
84
- def extract_video_frames(uploaded_video):
85
- """Extract frames from uploaded video for analysis"""
86
- try:
87
- tfile = io.BytesIO(uploaded_video.read())
88
- temp_filename = "temp_video.mp4"
89
- with open(temp_filename, "wb") as f:
90
- f.write(tfile.getvalue())
91
-
92
- cap = cv2.VideoCapture(temp_filename)
93
- frame_list = []
94
- frame_count = 0
95
-
96
- while cap.isOpened():
97
- ret, frame = cap.read()
98
- if not ret or frame_count > 10: # Process only up to 10 frames
99
- break
100
- frame_list.append(frame)
101
- frame_count += 1
102
-
103
- cap.release()
104
- os.remove(temp_filename)
105
- return frame_list
106
- except Exception as e:
107
- st.error(f"Video processing error: {str(e)}")
108
- return []
109
-
110
- def generate_pdf_report(report_text):
111
- """Generate PDF document from report text"""
112
- buffer = io.BytesIO()
113
- doc = SimpleDocTemplate(buffer, pagesize=letter)
114
- styles = getSampleStyleSheet()
115
- story = []
116
-
117
- title = Paragraph("<b>Rice Quality Report</b>", styles['Title'])
118
- story.append(title)
119
- story.append(Spacer(1, 12))
120
-
121
- content = Paragraph(report_text.replace('\n', '<br/>'), styles['BodyText'])
122
- story.append(content)
123
-
124
- doc.build(story)
125
- buffer.seek(0)
126
- return buffer
127
-
128
  def generate_rice_report(image_data, img_format, client):
129
  """Generate AI-powered rice quality analysis"""
130
  if not image_data:
@@ -139,12 +96,12 @@ def generate_rice_report(image_data, img_format, client):
139
  "role": "user",
140
  "content": [
141
  {"type": "text", "text": (
142
- "Analyze the rice grain image and provide a detailed report including:\n"
143
- "1. Rice type classification\n"
144
- "2. Quality assessment (broken grains %, discoloration %, impurities %)\n"
145
- "3. Foreign object detection\n"
146
- "4. Size and shape consistency\n"
147
- "5. Recommendations for processing or improvement"
148
  )},
149
  {"type": "image_url", "image_url": {"url": image_url}},
150
  ]
@@ -158,6 +115,12 @@ def generate_rice_report(image_data, img_format, client):
158
  st.error(f"API communication error: {str(e)}")
159
  return None
160
 
 
 
 
 
 
 
161
  def display_main_interface():
162
  """Render primary application interface"""
163
  st.title("🌾 Rice Quality Analyzer")
@@ -167,8 +130,8 @@ def display_main_interface():
167
  def render_sidebar(client):
168
  """Create sidebar interface elements"""
169
  with st.sidebar:
170
- st.subheader("Upload Image or Video")
171
- uploaded_file = st.file_uploader("Select an image or video", type=ALLOWED_FILE_TYPES + ALLOWED_VIDEO_TYPES)
172
 
173
  if uploaded_file:
174
  base64_image, img_format = process_image_data(uploaded_file)
@@ -180,7 +143,8 @@ def render_sidebar(client):
180
 
181
  if "analysis_result" in st.session_state:
182
  st.markdown("### πŸ“‹ Analysis Report")
183
- st.markdown(f'<div class="report-container"><div class="report-text">{st.session_state.analysis_result}</div></div>', unsafe_allow_html=True)
 
184
 
185
  def main():
186
  configure_application()
 
4
  import os
5
  import base64
6
  import io
7
+ import pandas as pd
8
  from dotenv import load_dotenv
9
  from groq import Groq
10
  from reportlab.lib.pagesizes import letter
 
82
  st.error(f"Image processing error: {str(e)}")
83
  return None, None
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  def generate_rice_report(image_data, img_format, client):
86
  """Generate AI-powered rice quality analysis"""
87
  if not image_data:
 
96
  "role": "user",
97
  "content": [
98
  {"type": "text", "text": (
99
+ "Analyze the rice grain image and provide a detailed report including:",
100
+ "Rice type classification",
101
+ "Quality assessment (broken grains %, discoloration %, impurities %)",
102
+ "Foreign object detection",
103
+ "Size and shape consistency",
104
+ "Recommendations for processing or improvement"
105
  )},
106
  {"type": "image_url", "image_url": {"url": image_url}},
107
  ]
 
115
  st.error(f"API communication error: {str(e)}")
116
  return None
117
 
118
+ def format_report_as_table(report_text):
119
+ """Convert report text into structured table format"""
120
+ rows = [row.split(': ') for row in report_text.split('\n') if ': ' in row]
121
+ df = pd.DataFrame(rows, columns=["Category", "Details"])
122
+ return df
123
+
124
  def display_main_interface():
125
  """Render primary application interface"""
126
  st.title("🌾 Rice Quality Analyzer")
 
130
  def render_sidebar(client):
131
  """Create sidebar interface elements"""
132
  with st.sidebar:
133
+ st.subheader("Upload Image")
134
+ uploaded_file = st.file_uploader("Select an image", type=ALLOWED_FILE_TYPES)
135
 
136
  if uploaded_file:
137
  base64_image, img_format = process_image_data(uploaded_file)
 
143
 
144
  if "analysis_result" in st.session_state:
145
  st.markdown("### πŸ“‹ Analysis Report")
146
+ report_df = format_report_as_table(st.session_state.analysis_result)
147
+ st.table(report_df)
148
 
149
  def main():
150
  configure_application()