上传文件至 /
This commit is contained in:
		
							parent
							
								
									b95385d6fe
								
							
						
					
					
						commit
						bae07550f7
					
				
							
								
								
									
										148
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										148
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,148 @@ | |||||||
|  | # 代码说明 | ||||||
|  | 
 | ||||||
|  | ## 导入必要的库: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `import cv2 | ||||||
|  | 
 | ||||||
|  | from ultralytics import YOLO` | ||||||
|  | 
 | ||||||
|  | ### 导入 OpenCV 库用于图像处理和显示。 | ||||||
|  | 
 | ||||||
|  | ### 导入 Ultralytics 的 YOLO 库用于目标检测。 | ||||||
|  | 
 | ||||||
|  | #### 定义边界线: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `def define_line(image): | ||||||
|  | 
 | ||||||
|  |     height, width = image.shape[:2] | ||||||
|  | 
 | ||||||
|  |     line_y = int(height * 0.5) | ||||||
|  | 
 | ||||||
|  |     return [(0, line_y), (width, line_y)]` | ||||||
|  | 
 | ||||||
|  | ### 这个函数用于定义视频中的一个边界线。这里定义的是视频中间位置的一条水平线。 | ||||||
|  | 
 | ||||||
|  | #### 检查是否越线: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `def check_crossing(bounding_box, line): | ||||||
|  | 
 | ||||||
|  |     y1, _ = bounding_box[0] | ||||||
|  | 
 | ||||||
|  |     y2, _ = bounding_box[1] | ||||||
|  | 
 | ||||||
|  |     _, ly = line[0] | ||||||
|  | 
 | ||||||
|  |     return y1 <= ly <= y2 or y2 <= ly <= y1` | ||||||
|  | 
 | ||||||
|  | ### 这个函数用于检查目标的边界框(bounding box)是否跨越了指定的边界线。如果边界框的任意一部分在边界线上方或下方,则认为该目标越线。 | ||||||
|  | 
 | ||||||
|  | #### 加载模型: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `1model = YOLO('yolov8n.pt')` | ||||||
|  | 
 | ||||||
|  | 加载预训练的 YOLOv8 模型。在这个例子中使用的是 yolov8n.pt,这是一个轻量级的模型。 | ||||||
|  | 
 | ||||||
|  | #### 读取视频: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `video_path = 'path/to/video.mp4'` | ||||||
|  | 
 | ||||||
|  | `cap = cv2.VideoCapture(video_path)` | ||||||
|  | 
 | ||||||
|  | 打开一个视频文件,准备逐帧读取。 | ||||||
|  | 
 | ||||||
|  | #### 主循环: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | 深色版本 | ||||||
|  | 
 | ||||||
|  | `while cap.isOpened(): | ||||||
|  |     ret, frame = cap.read() | ||||||
|  |     if not ret: | ||||||
|  |         break` | ||||||
|  | 
 | ||||||
|  | 这个循环会一直读取视频直到视频结束。 | ||||||
|  | 
 | ||||||
|  | #### 目标检测: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `results = model(frame)[0]` | ||||||
|  | 
 | ||||||
|  | 使用 YOLO 模型对每一帧进行目标检测,并获取结果。 | ||||||
|  | 
 | ||||||
|  | #### 定义边界线: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `line = define_line(frame)` | ||||||
|  | 
 | ||||||
|  | 对当前帧定义边界线。 | ||||||
|  | 
 | ||||||
|  | #### 画出边界线: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `1cv2.line(frame, line[0], line[1], (0, 255, 0), 2)` | ||||||
|  | 
 | ||||||
|  | 在视频帧上绘制绿色的边界线。 | ||||||
|  | 
 | ||||||
|  | #### 检测并标记越线目标: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `1for box in results.boxes.xyxy: | ||||||
|  | 
 | ||||||
|  |     x1, y1, x2, y2 = box.astype(int)` | ||||||
|  |      | ||||||
|  |     # 检查是否越线 | ||||||
|  | 
 | ||||||
|  |     `if check_crossing(((x1, y1), (x2, y2)), line):` | ||||||
|  | 
 | ||||||
|  |        ` color = (0, 0, 255) ` # 红色表示越线 | ||||||
|  |    `else: | ||||||
|  |        color = (255, 0, 0) ` # 蓝色表示未越线 | ||||||
|  |      | ||||||
|  |     # 画出边界框 | ||||||
|  | 
 | ||||||
|  |  ``   cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)`` | ||||||
|  | 
 | ||||||
|  | 遍历检测到的目标,并根据是否越线改变边界框的颜色。 | ||||||
|  | 
 | ||||||
|  | #### 显示帧: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | 深色版本 | ||||||
|  | 
 | ||||||
|  | `cv2.imshow('Frame', frame)` | ||||||
|  | 
 | ||||||
|  | 显示带有边界框和边界线的视频帧。 | ||||||
|  | 
 | ||||||
|  | #### 退出条件: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `if cv2.waitKey(1) & 0xFF == ord('q'): | ||||||
|  |     break` | ||||||
|  | 
 | ||||||
|  | 如果用户按下 'q' 键,则退出循环。 | ||||||
|  | 
 | ||||||
|  | #### 释放资源: | ||||||
|  | 
 | ||||||
|  | python | ||||||
|  | 
 | ||||||
|  | `cap.release() | ||||||
|  | cv2.destroyAllWindows()` | ||||||
|  | 
 | ||||||
|  | 释放视频捕获对象,并销毁所有窗口 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user