图片中行人检测GUI代码
时间: 2024-05-14 20:17:46 浏览: 81
以下是使用OpenCV实现行人检测GUI的示例代码:
```python
import cv2
import tkinter as tk
from PIL import Image, ImageTk
class PedestrianDetectorGUI:
def __init__(self, win):
self.win = win
self.win.title("Pedestrian Detector GUI")
self.canvas = tk.Canvas(win, width=640, height=480)
self.canvas.pack()
self.image = None
self.rectangles = []
# Load pedestrian detector model
self.hog = cv2.HOGDescriptor()
self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
# Create buttons
self.detect_button = tk.Button(win, text="Detect Pedestrians", command=self.detect_pedestrians)
self.detect_button.pack(side=tk.LEFT)
self.reset_button = tk.Button(win, text="Reset", command=self.reset)
self.reset_button.pack(side=tk.RIGHT)
# Create video capture object
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Start GUI main loop
self.update()
def update(self):
ret, frame = self.cap.read()
if ret:
# Convert frame to RGB
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Detect pedestrians
rectangles, weights = self.hog.detectMultiScale(frame, winStride=(8,8))
# Draw rectangles around pedestrians
for i, (x, y, w, h) in enumerate(rectangles):
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, f'Pedestrian {i+1}', (x-10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
# Update image on canvas
self.image = ImageTk.PhotoImage(Image.fromarray(frame))
self.canvas.create_image(0, 0, image=self.image, anchor=tk.NW)
# Save rectangles for reset button
self.rectangles = rectangles
# Schedule next update
self.win.after(10, self.update)
def detect_pedestrians(self):
# Alert user that detection is in progress
self.detect_button.config(state=tk.DISABLED)
self.detect_button.config(text="Detecting...")
# Capture current frame
ret, frame = self.cap.read()
# Detect pedestrians in current frame
rectangles, weights = self.hog.detectMultiScale(frame, winStride=(8,8))
# Draw rectangles around pedestrians
for i, (x, y, w, h) in enumerate(rectangles):
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame, f'Pedestrian {i+1}', (x-10, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
# Update image on canvas
self.image = ImageTk.PhotoImage(Image.fromarray(frame))
self.canvas.create_image(0, 0, image=self.image, anchor=tk.NW)
# Save rectangles for reset button
self.rectangles = rectangles
# Alert user that detection is complete
self.detect_button.config(state=tk.NORMAL)
self.detect_button.config(text="Detect Pedestrians")
def reset(self):
# Remove rectangles from current frame
frame = cv2.cvtColor(cv2.imread('blank.png'), cv2.COLOR_BGR2RGB)
for (x, y, w, h) in self.rectangles:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 0), -1)
# Update image on canvas
self.image = ImageTk.PhotoImage(Image.fromarray(frame))
self.canvas.create_image(0, 0, image=self.image, anchor=tk.NW)
if __name__ == '__main__':
win = tk.Tk()
app = PedestrianDetectorGUI(win)
win.mainloop()
```
该代码使用OpenCV实现行人检测,使用Tkinter构建GUI界面。代码实现的功能包括:
- 在GUI界面上显示摄像头捕获的视频流;
- 在视频流中检测行人,并用绿色矩形框标记出行人的位置和序号;
- 提供“检测行人”和“重置”两个按钮,分别用于开始行人检测和清除矩形框。
需要注意的是,该代码中使用的视频流来自于计算机内置的摄像头,如果要使用外置摄像头或者视频文件,需要对代码进行相应的修改。
阅读全文