Upload 2 files
Browse files- app.py +47 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from doclayout_yolo import YOLOv10
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
# Load the pre-trained model
|
9 |
+
model = YOLOv10("model/doclayout_yolo_docstructbench_imgsz1024.pt")
|
10 |
+
|
11 |
+
# Automatically select device
|
12 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
13 |
+
st.write(f"Using device: {device}")
|
14 |
+
|
15 |
+
# Streamlit UI
|
16 |
+
st.title("Document Layout Detection")
|
17 |
+
st.subheader("Upload an image to detect and annotate document layout")
|
18 |
+
|
19 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
20 |
+
|
21 |
+
if uploaded_file is not None:
|
22 |
+
# Display the uploaded image
|
23 |
+
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
24 |
+
|
25 |
+
# Load the uploaded image
|
26 |
+
image = Image.open(uploaded_file).convert("RGB")
|
27 |
+
image_path = "temp_input.jpg" # Temporary save for inference
|
28 |
+
image.save(image_path)
|
29 |
+
|
30 |
+
# Perform prediction
|
31 |
+
with st.spinner("Processing..."):
|
32 |
+
det_res = model.predict(
|
33 |
+
image_path,
|
34 |
+
imgsz=1024,
|
35 |
+
conf=0.2,
|
36 |
+
device=device,
|
37 |
+
)
|
38 |
+
|
39 |
+
# Annotate the result
|
40 |
+
annotated_frame = det_res[0].plot(pil=True, line_width=5, font_size=20)
|
41 |
+
|
42 |
+
# Convert annotated PIL image to displayable format
|
43 |
+
annotated_image = np.array(annotated_frame)
|
44 |
+
|
45 |
+
# Display the annotated image
|
46 |
+
st.image(annotated_image, caption="Annotated Image", use_container_width=True)
|
47 |
+
st.success("Detection completed!")
|
requirements.txt
ADDED
Binary file (2.97 kB). View file
|
|