vprashant commited on
Commit
d9b99b8
·
verified ·
1 Parent(s): b2f85dd

update card

Browse files
Files changed (1) hide show
  1. README.md +111 -0
README.md ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ base_model:
6
+ - jameslahm/yolov10n
7
+ ---
8
+
9
+
10
+ ```markdown
11
+ # Document Layout Detection
12
+
13
+ This script demonstrates how to use the document layout detection model on an image.
14
+ Below are the steps and code implementation.
15
+
16
+ ---
17
+
18
+ ## Step 1: Import Required Libraries
19
+ ```python
20
+ import cv2
21
+ import matplotlib.pyplot as plt
22
+ import numpy as np
23
+ from ultralytics import YOLOv10
24
+ from google.colab.patches import cv2_imshow
25
+ ```
26
+
27
+ - **cv2**: For image processing.
28
+ - **matplotlib.pyplot**: For plotting if needed.
29
+ - **numpy**: For numerical operations.
30
+ - **YOLOv10**: For object detection.
31
+ - **cv2_imshow**: For displaying images in Google Colab.
32
+
33
+ ---
34
+
35
+ ## Step 2: Load YOLOv10 Model
36
+ ```python
37
+ model = YOLOv10('vprashant/doclayout_detector/weights')
38
+ ```
39
+
40
+ - Load the YOLOv10 model with the path to your trained weights.
41
+
42
+ ---
43
+
44
+ ## Step 3: Read and Prepare the Image
45
+ ```python
46
+ img = cv2.imread('/content/yolov10/dataset/train/images/11.png')
47
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
48
+ ```
49
+
50
+ - Read the image from the specified path.
51
+ - Convert the image from BGR to RGB color space.
52
+
53
+ ---
54
+
55
+ ## Step 4: Perform Object Detection
56
+ ```python
57
+ results = model(img)
58
+ ```
59
+
60
+ - Run the YOLOv10 model on the image to get detection results.
61
+
62
+ ---
63
+
64
+ ## Step 5: Extract and Process Detection Results
65
+ ```python
66
+ results = results[0]
67
+ boxes = results.boxes
68
+ data = boxes.data.cpu().numpy()
69
+ ```
70
+
71
+ - Extract the first result (image-level result).
72
+ - Access the detected bounding boxes.
73
+ - Convert detection data to a NumPy array for processing.
74
+
75
+ ---
76
+
77
+ ## Step 6: Visualize Results
78
+ ```python
79
+ for i, detection in enumerate(data):
80
+ x1, y1, x2, y2, conf, cls_id = detection
81
+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2]) # Convert coordinates to integers
82
+ cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw bounding box
83
+
84
+ class_name = model.names[int(cls_id)] # Get class name
85
+ label = f"{class_name}: {conf:.2f}" # Create label with confidence score
86
+ cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX,
87
+ 0.9, (0, 255, 0), 2) # Add text label
88
+ ```
89
+
90
+ - Loop through all detections.
91
+ - Draw bounding boxes and labels on the image.
92
+
93
+ ---
94
+
95
+ ## Step 7: Display the Processed Image
96
+ ```python
97
+ cv2_imshow(img)
98
+ cv2.waitKey(0)
99
+ cv2.destroyAllWindows()
100
+ ```
101
+
102
+ - Display the image with detections in Google Colab using `cv2_imshow`.
103
+ - Wait for a keypress and close any OpenCV windows.
104
+
105
+ ---
106
+
107
+ ## Note
108
+ - Ensure you have the trained YOLOv10 model and the dataset in the specified paths.
109
+ - Replace the paths with your local or Colab paths.
110
+ - Install necessary libraries like OpenCV, Matplotlib, and ultralytics if not already installed.
111
+ ```