Final update
Browse files
app.py
CHANGED
|
@@ -4,12 +4,15 @@ import openpyxl
|
|
| 4 |
from openpyxl.styles import Font, Alignment
|
| 5 |
import tempfile
|
| 6 |
from roboflow import Roboflow
|
|
|
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
import cv2
|
| 9 |
import numpy as np
|
| 10 |
import os
|
| 11 |
from gradio_calendar import Calendar
|
| 12 |
import datetime
|
|
|
|
| 13 |
|
| 14 |
excel_tempfile_state = gr.State()
|
| 15 |
roll_number_state = gr.State()
|
|
@@ -19,6 +22,33 @@ sno_state.value=1
|
|
| 19 |
|
| 20 |
str_nums = {"zero":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9}
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def task1(Examination, Date_Of_Exam, Program, Branch, Course, Name_Of_Faculty, Academic_Year):
|
| 23 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as temp_file:
|
| 24 |
workbook = openpyxl.Workbook()
|
|
@@ -194,24 +224,51 @@ iface1 = gr.Interface(
|
|
| 194 |
)
|
| 195 |
|
| 196 |
|
| 197 |
-
def predict_and_crop(image_np, api_key,
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Function to resize and insert cropped image
|
| 217 |
def resize_and_insert(cropped_image, output_image_path):
|
|
@@ -274,18 +331,21 @@ def append_to_workbook(cells_data, excel_file_path):
|
|
| 274 |
def task2(image_np):
|
| 275 |
api_key = "UyAumhQJOJpo7vUu3LaK"
|
| 276 |
project_name = "marks_table_detection_lbrce"
|
|
|
|
| 277 |
model_version = 1
|
| 278 |
-
base_image_path = "base_img.png"
|
| 279 |
-
temp_image_path = "temp_image.jpg"
|
| 280 |
-
output_image_path = "merged_image.jpg"
|
| 281 |
|
| 282 |
#API and requirements for OCR Model
|
| 283 |
-
rf = Roboflow(api_key="XsMt3y86MNDGihOYcWDY")
|
| 284 |
-
project = rf.workspace().project("mnist-cjkff")
|
| 285 |
-
model = project.version(2).model
|
|
|
|
| 286 |
|
| 287 |
# Predict and crop
|
| 288 |
-
cropped_image, original_image = predict_and_crop(image_np, api_key, project_name, model_version)
|
|
|
|
| 289 |
|
| 290 |
# Resize and insert
|
| 291 |
result_image = resize_and_insert(cropped_image, output_image_path)
|
|
@@ -305,12 +365,33 @@ def task2(image_np):
|
|
| 305 |
_, temp_thresholded_path = tempfile.mkstemp(suffix=".jpg")
|
| 306 |
cv2.imwrite(temp_thresholded_path, thresholded_cell)
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
qno += 1
|
| 315 |
excel_file_path = excel_tempfile_state.value
|
| 316 |
append_to_workbook(cells_data,excel_file_path)
|
|
@@ -336,6 +417,4 @@ iface2 = gr.Interface(
|
|
| 336 |
demo = gr.TabbedInterface([iface1, iface2], ["Configure Excel Sheet Data", "Extract marks from Answer Sheets"])
|
| 337 |
|
| 338 |
# Run the interface
|
| 339 |
-
demo.launch(share=True,debug=True)
|
| 340 |
-
|
| 341 |
-
|
|
|
|
| 4 |
from openpyxl.styles import Font, Alignment
|
| 5 |
import tempfile
|
| 6 |
from roboflow import Roboflow
|
| 7 |
+
from inference import get_model
|
| 8 |
+
import supervision as sv
|
| 9 |
from PIL import Image
|
| 10 |
import cv2
|
| 11 |
import numpy as np
|
| 12 |
import os
|
| 13 |
from gradio_calendar import Calendar
|
| 14 |
import datetime
|
| 15 |
+
import google.generativeai as genai
|
| 16 |
|
| 17 |
excel_tempfile_state = gr.State()
|
| 18 |
roll_number_state = gr.State()
|
|
|
|
| 22 |
|
| 23 |
str_nums = {"zero":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9}
|
| 24 |
|
| 25 |
+
genai.configure(api_key="AIzaSyBXu8JrmDtmuK0FQCGZMyB3T-hb35UCtGM")
|
| 26 |
+
|
| 27 |
+
def upload_to_gemini(path, mime_type=None):
|
| 28 |
+
try:
|
| 29 |
+
file = genai.upload_file(path, mime_type=mime_type)
|
| 30 |
+
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
| 31 |
+
return file
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error uploading file: {e}")
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
# Create the model
|
| 37 |
+
generation_config = {
|
| 38 |
+
"temperature": 1,
|
| 39 |
+
"top_p": 0.95,
|
| 40 |
+
"top_k": 64,
|
| 41 |
+
"max_output_tokens": 8192,
|
| 42 |
+
"response_mime_type": "text/plain",
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
model = genai.GenerativeModel(
|
| 47 |
+
model_name="gemini-2.0-flash-lite-preview-02-05",
|
| 48 |
+
generation_config=generation_config,
|
| 49 |
+
system_instruction="Identify the single handwritten digit on the given image, some times there will be blank images given.In that case give blank as output",
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
def task1(Examination, Date_Of_Exam, Program, Branch, Course, Name_Of_Faculty, Academic_Year):
|
| 53 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as temp_file:
|
| 54 |
workbook = openpyxl.Workbook()
|
|
|
|
| 224 |
)
|
| 225 |
|
| 226 |
|
| 227 |
+
def predict_and_crop(image_np, api_key, model_id, confidence=40, overlap=30):
|
| 228 |
+
# Convert the image to the format expected by the model (e.g., numpy array)
|
| 229 |
+
image = image_np
|
| 230 |
+
|
| 231 |
+
# Get the Roboflow model and run inference
|
| 232 |
+
model = get_model(model_id=model_id, api_key=api_key)
|
| 233 |
+
results = model.infer(image)[0]
|
| 234 |
+
|
| 235 |
+
# Print and process results
|
| 236 |
+
print("Results:", results)
|
| 237 |
+
|
| 238 |
+
# Load the results into the supervision Detections API
|
| 239 |
+
detections = sv.Detections.from_inference(results)
|
| 240 |
+
print("detections:", detections)
|
| 241 |
+
|
| 242 |
+
# Create supervision annotators
|
| 243 |
+
bounding_box_annotator = sv.BoxAnnotator()
|
| 244 |
+
label_annotator = sv.LabelAnnotator()
|
| 245 |
+
|
| 246 |
+
# Annotate the image with bounding boxes and labels
|
| 247 |
+
annotated_image = bounding_box_annotator.annotate(scene=image, detections=detections)
|
| 248 |
+
annotated_image = label_annotator.annotate(scene=annotated_image, detections=detections)
|
| 249 |
+
|
| 250 |
+
# Display the image with annotations
|
| 251 |
+
sv.plot_image(annotated_image)
|
| 252 |
+
|
| 253 |
+
# Now, extract the bounding box details from detections for cropping
|
| 254 |
+
for detection in detections.xyxy:
|
| 255 |
+
# Extract coordinates from detections (xyxy format: [x1, y1, x2, y2])
|
| 256 |
+
x1, y1, x2, y2 = map(int, detection)
|
| 257 |
+
|
| 258 |
+
# Crop the image based on the bounding box
|
| 259 |
+
cropped_img = image[y1:y2, x1:x2]
|
| 260 |
+
|
| 261 |
+
# Convert cropped image to PIL for rotation handling (if needed)
|
| 262 |
+
cropped_img_pil = Image.fromarray(cropped_img)
|
| 263 |
+
cropped_img_np = np.array(cropped_img_pil)
|
| 264 |
+
|
| 265 |
+
# Rotate the image if height is greater than width
|
| 266 |
+
h, w, c = cropped_img_np.shape
|
| 267 |
+
if h > w:
|
| 268 |
+
cropped_img_np = cv2.rotate(cropped_img_np, cv2.ROTATE_90_CLOCKWISE)
|
| 269 |
+
print(cropped_img_np)
|
| 270 |
+
return cropped_img_np, image # Return both cropped image and original image
|
| 271 |
+
|
| 272 |
|
| 273 |
# Function to resize and insert cropped image
|
| 274 |
def resize_and_insert(cropped_image, output_image_path):
|
|
|
|
| 331 |
def task2(image_np):
|
| 332 |
api_key = "UyAumhQJOJpo7vUu3LaK"
|
| 333 |
project_name = "marks_table_detection_lbrce"
|
| 334 |
+
model_id = "marks_table_detection_lbrce/2"
|
| 335 |
model_version = 1
|
| 336 |
+
base_image_path = "/content/base_img.png"
|
| 337 |
+
temp_image_path = "/content/temp_image.jpg"
|
| 338 |
+
output_image_path = "/content/merged_image.jpg"
|
| 339 |
|
| 340 |
#API and requirements for OCR Model
|
| 341 |
+
# rf = Roboflow(api_key="XsMt3y86MNDGihOYcWDY")
|
| 342 |
+
# project = rf.workspace().project("mnist-cjkff")
|
| 343 |
+
# model = project.version(2).model
|
| 344 |
+
# model = get_model(model_id="mnist-cjkff", api_key="XsMt3y86MNDGihOYcWDY")
|
| 345 |
|
| 346 |
# Predict and crop
|
| 347 |
+
# cropped_image, original_image = predict_and_crop(image_np, api_key, project_name, model_version)
|
| 348 |
+
cropped_image, original_image = predict_and_crop(image_np, api_key, model_id)
|
| 349 |
|
| 350 |
# Resize and insert
|
| 351 |
result_image = resize_and_insert(cropped_image, output_image_path)
|
|
|
|
| 365 |
_, temp_thresholded_path = tempfile.mkstemp(suffix=".jpg")
|
| 366 |
cv2.imwrite(temp_thresholded_path, thresholded_cell)
|
| 367 |
|
| 368 |
+
image_path = "/content/temp_image_12.jpg" # Make sure the image is in the correct path
|
| 369 |
+
mime_type = "image/jpeg" # Use the appropriate MIME type
|
| 370 |
+
|
| 371 |
+
# Upload the image to Gemini
|
| 372 |
+
file = upload_to_gemini(temp_thresholded_path, mime_type)
|
| 373 |
+
|
| 374 |
+
# Start a chat session and send the image as input
|
| 375 |
+
chat_session = model.start_chat(
|
| 376 |
+
history=[
|
| 377 |
+
{
|
| 378 |
+
"role": "user",
|
| 379 |
+
"parts": [
|
| 380 |
+
file,
|
| 381 |
+
"Identify the digit",
|
| 382 |
+
],
|
| 383 |
+
},
|
| 384 |
+
]
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
# Send a message to process the image
|
| 388 |
+
response = chat_session.send_message("Identify the digit")
|
| 389 |
+
print(response.text) # Output the model's response
|
| 390 |
+
dt = response.text[::2]
|
| 391 |
+
if dt.isdigit():
|
| 392 |
+
cells_data.append(int(dt))
|
| 393 |
+
else:
|
| 394 |
+
cells_data.append(0)
|
| 395 |
qno += 1
|
| 396 |
excel_file_path = excel_tempfile_state.value
|
| 397 |
append_to_workbook(cells_data,excel_file_path)
|
|
|
|
| 417 |
demo = gr.TabbedInterface([iface1, iface2], ["Configure Excel Sheet Data", "Extract marks from Answer Sheets"])
|
| 418 |
|
| 419 |
# Run the interface
|
| 420 |
+
demo.launch(share=True,debug=True)
|
|
|
|
|
|