Skip to content

Commit 94cb6f7

Browse files
committed
Updated detect-trees.py
1 parent 990b9a9 commit 94cb6f7

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed
539 Bytes
Binary file not shown.

scripts/detect-trees.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
parser.add_argument("--input", "-iv", required=True, help="The location of the input video file")
1111
parser.add_argument("--output", "-ov", required=True, help="The location of the output video file")
12-
parser.add_argument("--config", "-cfg", required=False, help="The model configuration file")
13-
parser.add_argument("--weights", "-w", required=False, help="The model weights file")
14-
parser.add_argument("--classes", "-c", required=False, help="The file containing the class descriptions")
12+
parser.add_argument("--config", "-cfg", required=False, default="models/yolov4/yolov4-custom.cfg", help="The model configuration file")
13+
parser.add_argument("--weights", "-w", required=False, default="models/yolov4/yolov4-custom_4000.weights", help="The model weights file")
14+
parser.add_argument("--classes", "-c", required=False, default="models/yolov4/classes-3.names", help="The file containing the class descriptions")
1515
args = parser.parse_args()
1616

1717
# Default usage
@@ -22,13 +22,18 @@
2222
-cfg models/yolov3/cfg/yolov3_custom.cfg \
2323
-w models/yolov3/weights/yolov3_custom_final.weights \
2424
-c models/yolov3/classes.names
25+
26+
OR
27+
28+
python3 scripts/detect-trees.py -iv videos/video1.mp4 -ov videos/test.mp4
29+
2530
"""
2631
#---------------------------------------------------------
2732

2833
# set the VideoCapture and VideoWriter objects------------
2934
cap = cv2.VideoCapture(str(args.input))
3035

31-
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
36+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
3237
out = cv2.VideoWriter(str(args.output),fourcc, 25.0, (800,600))
3338
#---------------------------------------------------------
3439

@@ -44,7 +49,7 @@
4449
frameSkip = 25
4550
#---------------------------------------------------------
4651

47-
# LOS configurations--------------------------------------
52+
# LOS configurations (If the centroid crosses this line, count it)
4853
line_start = (0, 500)
4954
line_end = (800, 500)
5055

@@ -58,7 +63,6 @@
5863
#---------------------------------------------------------
5964

6065
# Load the network----------------------------------------
61-
#net = cv2.dnn.readNetFromDarknet(config, weights)
6266
net = cv2.dnn.readNetFromDarknet(config, weights)
6367
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
6468
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
@@ -72,12 +76,13 @@
7276
# Capture frame-by-frame
7377
ret, frame = cap.read()
7478
img = cv2.resize(frame,(800, 600))
75-
# Our operations on the frame come here
76-
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
79+
7780
height, width= img.shape[:2]
7881

7982
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), swapRB=True, crop=False)
8083
net.setInput(blob)
84+
85+
# Perform a forward pass through the net
8186
layer_outputs = net.forward(output_layers)
8287

8388
class_ids, confidences, b_boxes = [], [], []
@@ -94,11 +99,11 @@
9499
x = int(center_x - w / 2)
95100
y = int(center_y - h / 2)
96101
b_boxes.append([x, y, int(w), int(h)])
102+
103+
# Obtain the centroids for each bounding box
97104
centroid_x, centroid_y = utils.get_centroid(x, y, x+int(w), y+int(h))
98105
centroids.append([centroid_x, centroid_y])
99-
# Add a horizontal line, and count
100-
101-
106+
102107
confidences.append(float(confidence))
103108
class_ids.append(int(class_id))
104109

@@ -110,22 +115,28 @@
110115
classes = [line.strip() for line in f.readlines()]
111116
colors = np.random.uniform(0, 255, size=(len(classes), 3))
112117

118+
113119
for index in indices:
114120
x, y, w, h = b_boxes[index]
115121
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
116122
cv2.putText(img, classes[class_ids[index]], (x - 10, y - 5), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 2)
117123

118124
for c in centroids:
119125
c_x, c_y = c[0], c[1]
120-
cv2.circle(img, (c_x, c_y), radius=2, color=(0, 255, 255), thickness=2)
126+
cv2.circle(img, (c_x, c_y), radius=2, color=(0, 255, 255), thickness=1)
121127
cv2.line(img, line_start, line_end, line_color, line_thickness)
122128

123-
#if(c_y > 500):
129+
# This is a VERY stupid way of counting
130+
# as it will count the same object in multiple
131+
# frames
132+
#if(c_y > 500):
124133
# treeNum = treeNum + 1
125134

126135
#cv2.putText(img, str(treeNum), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (128, 120, 68), 2)
127136

137+
# Show a red line for the distance from the centroid to the counting line(at 500 px height)
128138
cv2.line(img, (c_x, c_y), (c_x, 500), alt_line_color, line_thickness)
139+
129140
# Display the resulting frame
130141
cv2.imshow('image',img)
131142
out.write(img)

0 commit comments

Comments
 (0)