在进行过目标检测的训练后,我们如果想要实现划分区域检测,需要在detect.py文件里面添加一些代码来实现。
1.我们需要在#Run inference模块里修改:
在for path, img, im0s, vid_cap, s in dataset:这句代码的后面添加如下代码:大家可以设置不同点框出各种各样的区域形状。
# Run inference
model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz)) # warmup
seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
for path, img, im0s, vid_cap, s in dataset:
# mask for certain region
# 1,2,3,4 分别对应左上,右上,右下,左下四个点
hl1 = 1.6 / 10 # 监测区域高度距离图片顶部比例
wl1 = 6.4 / 10 # 监测区域高度距离图片左部比例
hl2 = 1.6 / 10 # 监测区域高度距离图片顶部比例
wl2 = 6.8 / 10 # 监测区域高度距离图片左部比例
hl3 = 4.5 / 10 # 监测区域高度距离图片顶部比例
wl3 = 7.6 / 10 # 监测区域高度距离图片左部比例
hl4 = 4.5 / 10 # 监测区域高度距离图片顶部比例
wl4 = 5.5 / 10 # 监测区域高度距离图片左部比例
if webcam:
for b in range(0, img.shape[0]):
mask = np.zeros([img[b].shape[1], img[b].shape[2]], dtype=np.uint8)
# mask[round(img[b].shape[1] * hl1):img[b].shape[1], round(img[b].shape[2] * wl1):img[b].shape[2]] = 255
pts = np.array([[int(img[b].shape[2] * wl1), int(img[b].shape[1] * hl1)], # pts1
[int(img[b].shape[2] * wl2), int(img[b].shape[1] * hl2)], # pts2
[int(img[b].shape[2] * wl3), int(img[b].shape[1] * hl3)], # pts3
[int(img[b].shape[2] * wl4), int(img[b].shape[1] * hl4)]], np.int32)
mask = cv2.fillPoly(mask, [pts], (255, 255, 255))
imgc = img[b].transpose((1, 2, 0))
imgc = cv2.add(imgc, np.zeros(np.shape(imgc), dtype=np.uint8), mask=mask)
# cv2.imshow('1',imgc)
img[b] = imgc.transpose((2, 0, 1))
else:
mask = np.zeros([img.shape[1], img.shape[2]], dtype=np.uint8)
# mask[round(img.shape[1] * hl1):img.shape[1], round(img.shape[2] * wl1):img.shape[2]] = 255
pts = np.array([[int(img.shape[2] * wl1), int(img.shape[1] * hl1)], # pts1
[int(img.shape[2] * wl2), int(img.shape[1] * hl2)], # pts2
[int(img.shape[2] * wl3), int(img.shape[1] * hl3)], # pts3
[int(img.shape[2] * wl4), int(img.shape[1] * hl4)]], np.int32)
mask = cv2.fillPoly(mask, [pts], (255, 255, 255))
img = img.transpose((1, 2, 0))
img = cv2.add(img, np.zeros(np.shape(img), dtype=np.uint8), mask=mask)
img = img.transpose((2, 0, 1))
2.(这步修改可以把设置的划分区域绘制出来)在# Process predictions模块中添加如下代码:
# Process predictions
for i, det in enumerate(pred): # per image
seen += 1
if webcam: # batch_size >= 1
p, s, im0, frame = path[i], f'{i}: ', im0s[i].copy(), dataset.count
cv2.putText(im0, "Detection_Region", (int(im0.shape[1] * wl1 - 5), int(im0.shape[0] * hl1 - 5)),
cv2.FONT_HERSHEY_SIMPLEX,
1.0, (255, 255, 0), 2, cv2.LINE_AA)
pts = np.array([[int(im0.shape[1] * wl1), int(im0.shape[0] * hl1)], # pts1
[int(im0.shape[1] * wl2), int(im0.shape[0] * hl2)], # pts2
[int(im0.shape[1] * wl3), int(im0.shape[0] * hl3)], # pts3
[int(im0.shape[1] * wl4), int(im0.shape[0] * hl4)]], np.int32) # pts4
# pts = pts.reshape((-1, 1, 2))
zeros = np.zeros((im0.shape), dtype=np.uint8)
mask = cv2.fillPoly(zeros, [pts], color=(0, 165, 255))
im0 = cv2.addWeighted(im0, 1, mask, 0.2, 0)
cv2.polylines(im0, [pts], True, (255, 255, 0), 3)
# plot_one_box(dr, im0, label='Detection_Region', color=(0, 255, 0), line_thickness=2)
else:
p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
cv2.putText(im0, "Detection_Region", (int(im0.shape[1] * wl1 - 5), int(im0.shape[0] * hl1 - 5)),
cv2.FONT_HERSHEY_SIMPLEX,
1.0, (255, 255, 0), 2, cv2.LINE_AA)
pts = np.array([[int(im0.shape[1] * wl1), int(im0.shape[0] * hl1)], # pts1
[int(im0.shape[1] * wl2), int(im0.shape[0] * hl2)], # pts2
[int(im0.shape[1] * wl3), int(im0.shape[0] * hl3)], # pts3
[int(im0.shape[1] * wl4), int(im0.shape[0] * hl4)]], np.int32) # pts4
# pts = pts.reshape((-1, 1, 2))
zeros = np.zeros((im0.shape), dtype=np.uint8)
mask = cv2.fillPoly(zeros, [pts], color=(0, 165, 255))
im0 = cv2.addWeighted(im0, 1, mask, 0.2, 0)
cv2.polylines(im0, [pts], True, (255, 255, 0), 3)
3.下面开始对识别到的目标进行计数,在第2步紧接着的p = Path(p) # to Path这行代码前面添加:
global person_count
person_count = 0
接着在if save_img or save_crop or view_img: # Add bbox to image这行代码的后面添加:
if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
annotator.box_label(xyxy, label, color=colors(c, True))
person_count += 1 #新添加的代码
接着下面的两个模块的代码的修改如代码显示,其中新增的部分添加的有注释。
# Stream results
im0 = annotator.result()
if view_img:
text = 'person_num:%d ' % (person_count) # 新增代码
cv2.putText(im0, text, (180, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3) # 新增代码
cv2.namedWindow("Webcam", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Webcam", 1280, 720)
cv2.moveWindow("Webcam", 0, 100)
cv2.imshow("Webcam", im0)
cv2.waitKey(1)
# Save results (image with detections)
if save_img:
text = 'person_num:%d ' % (person_count) # 新增代码
cv2.putText(im0, text, (180, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3) # 新增代码
if dataset.mode == 'image':
cv2.imwrite(save_path, im0)
else: # 'video' or 'stream'
if vid_path[i] != save_path: # new video
vid_path[i] = save_path
if isinstance(vid_writer[i], cv2.VideoWriter):
vid_writer[i].release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, im0.shape[1], im0.shape[0]
save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer[i].write(im0)
# Print time (inference-only)
LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")
我们还需要导入需要的包:
import numpy as np
import cv2
这样我们就完成了划分区域检测并实现目标计数。
4.效果展示:
原图展示:
划分区域检测效果图: