results = self.model_predict(frame) dets = [] if len(results) == 0: pass else: for index, row in results.iterrows(): class_ids = row['class'] scores = float(row['confidence']) x1 = int(row['xmin']) y1 = int(row['ymin']) x2 = int(row['xmax']) y2 = int(row['ymax']) cx = int((x1 + x2)/2) cy = int((y1 + y2)/2) pt_center = (cx,cy) dist = cv2.pointPolygonTest(pts, pt_center, False) if dist > 0: print('在内部') dets.append([x1,y1,x2,y2,scores]) else: print('在外部') 找出为外部的,删除result值
时间: 2024-02-10 14:32:26 浏览: 32
您的问题是如何找出为外部的并删除result值?
在上述代码中,找出为外部的结果是通过计算目标中心点到多边形的距离(dist)来实现的,如果距离大于0,则说明目标不在多边形内部,需要将其删除。可以使用以下代码来实现:
```
to_delete = []
for index, row in results.iterrows():
class_ids = row['class']
scores = float(row['confidence'])
x1 = int(row['xmin'])
y1 = int(row['ymin'])
x2 = int(row['xmax'])
y2 = int(row['ymax'])
cx = int((x1 + x2)/2)
cy = int((y1 + y2)/2)
pt_center = (cx,cy)
dist = cv2.pointPolygonTest(pts, pt_center, False)
if dist > 0:
print('在外部')
to_delete.append(index)
else:
print('在内部')
results = results.drop(to_delete)
```
这段代码会遍历所有的结果,如果发现目标不在多边形内部,则将其对应的索引添加到一个列表中,遍历完所有结果后,再将这些索引对应的结果从results中删除。
相关问题
self.about_frame = AboutFrame(self.root) self.log_frame = LogFrame(self.root) menubar = tk.Menu(self.root) menubar.add_command(label='预测', command=self.show_predict) menubar.add_command(label='查询', command=self.show_log) menubar.add_command(label='关于', command=self.show_about) self.root['menu'] = menubar # self.predict_frame = tk.Frame(self.root).pack()为链式结构,实际上将predict_frame变量赋值为None self.predict_frame = tk.Frame(self.root) self.image_label = tk.Label(self.predict_frame) self.image_label.grid(row=1, column=0, pady=10) # pic_path更新 self.text_var.set(self.pic_path) # tk.Label(self.predict_frame, textvariable=self.text_var).grid(row=0, column=0, pady=10) tk.Button(self.predict_frame, text='预测', command=lambda: self.predict_button(self.pic_path), padx=30, pady=20).grid(row=1, column=1, padx=50, pady=10) tk.Button(self.predict_frame, text='预测', command=lambda: self.predict_button(self.pic_path), padx=30, pady=20).grid(row=2, column=1, padx=50, pady=10) tk.Button(self.predict_frame, text='读取文件', command=lambda: self.update_image(self.image_label), padx=30, pady=20).grid(row=1, column=2, padx=10, pady=10) self.predict_frame.pack() # 在predict_frame中内嵌条形图 self.fig = Figure(figsize=(5, 3), dpi=100) self.ax = self.fig.add_subplot(111) self.canvas = FigureCanvasTkAgg(self.fig, master=self.predict_frame) # columnspan用于指明占用多列 self.canvas.get_tk_widget().grid(row=3, column=0, columnspan=3)添加拍摄功能,并将拍摄图像在image_label中展示
要添加拍摄功能,并将拍摄图像在`image_label`中展示,可以使用`cv2`库读取摄像头拍摄的图像,并将其转换为`Tkinter`图像格式。以下是一个示例代码:
```
import cv2
from PIL import Image, ImageTk
# 添加拍摄功能的函数
def capture_image():
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cap.release()
if ret:
# 将OpenCV图像转换为PIL图像
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
# 将PIL图像转换为Tkinter图像
tk_image = ImageTk.PhotoImage(pil_image)
# 在image_label中展示图像
image_label.config(image=tk_image)
image_label.image = tk_image
```
在上述代码中,`capture_image()`函数使用`cv2.VideoCapture()`函数读取摄像头拍摄的图像。然后,使用`PIL`库将OpenCV图像转换为PIL图像,并使用`ImageTk`模块将其转换为Tkinter图像。最后,将Tkinter图像展示在`image_label`中。
要将`capture_image()`函数添加到界面中,可以在`predict_frame`中添加一个`Button`,并将其命令设置为`capture_image`函数:
```
tk.Button(self.predict_frame, text='拍摄', command=capture_image, padx=30, pady=20).grid(row=2, column=2, padx=10, pady=10)
```
这将在预测界面中添加一个名为“拍摄”的按钮,单击该按钮将调用`capture_image()`函数,并在`image_label`中展示拍摄的图像。
self.predict_y = self.reg.predict(_X) AttributeError: 'NoneType' object has no attribute 'predict'
This error message suggests that the object "self.reg" is of type "NoneType", which means it has no attribute called "predict". Therefore, when the code tries to call the "predict" method on "self.reg", it raises an AttributeError.
To fix this error, you need to make sure that "self.reg" is initialized properly and is not None. You can check if "self.reg" is None by adding a print statement before the line that raises the error:
```
print(self.reg) # add this line to check if self.reg is None
self.predict_y = self.reg.predict(_X)
```
If the output of the print statement is "None", then you need to initialize "self.reg" before calling the "predict" method. For example, if you are using scikit-learn's linear regression model, you can initialize "self.reg" as follows:
```
from sklearn.linear_model import LinearRegression
class MyModel:
def __init__(self):
self.reg = LinearRegression()
def fit(self, X, y):
self.reg.fit(X, y)
def predict(self, X):
_X = self._transform(X)
self.predict_y = self.reg.predict(_X)
return self.predict_y
```
This initializes "self.reg" to a new instance of the LinearRegression class, which has the "predict" method you need to call later.