**已关闭。**此问题是not reproducible or was caused by typos。目前不接受答案。
这个问题是由一个错字或一个无法再重现的问题引起的。虽然类似的问题可能在这里是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
上个月关门了。
Improve this question
我在使用OpenCV(我使用的是www.example.com版本)定义对象的中心时遇到了问题4.5.5.64。我使用“coco.names”进行对象识别,我的代码中有一个函数“show_distance”,它被设计用来计算识别对象的边界框的中心。但是中心的计算完全错误。如果对象在屏幕的左侧,框的中心几乎是正确的位置,但是当对象向右移动时,下面我提供了项目代码、GitHub上的项目文件和截图。
验证码:
import cv2
thres = 0.45 # Threshold to detect object
cap = cv2.VideoCapture(0)
CLASS_FILE = "coco.names"
CONFIG_PATH = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
WEIGHTS_PATH = 'frozen_inference_graph.pb'
TEST_OBJECT = 'person'
cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 70)
def show_distance(img, box, screen_center_x, screen_center_y):
obj_center_x = (box[0] + box[2]) // 2
obj_center_y = (box[1] + box[3]) // 2
distance_x = obj_center_x - screen_center_x
distance_y = obj_center_y - screen_center_y
text = f"Dist X: {distance_x}, Dist Y: {distance_y}"
cv2.putText(img, text, (box[0] + 10, box[1] - 10),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 255, 0), 1)
cv2.circle(img, (obj_center_x, obj_center_y), 10, (0, 255, 0), -1)
cv2.circle(img, (screen_center_x, screen_center_y), 10, (0, 0, 255))
def add_one_object(img, box, current_obj, confidence):
color = (0, 255, 0)
cv2.rectangle(img, box, color, thickness=2)
cv2.putText(
img, current_obj.upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
def main():
classNames = []
with open(CLASS_FILE, 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
net = cv2.dnn_DetectionModel(WEIGHTS_PATH, CONFIG_PATH)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
entered_name=TEST_OBJECT
while True:
success, img = cap.read()
classIds, confs, bbox = net.detect(img, confThreshold=thres)
if len(classIds) != 0:
screen_center_x = img.shape[1] // 2
screen_center_y = img.shape[0] // 2
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
if classNames[classId - 1].lower() == entered_name:
add_one_object(img, box, classNames[classId - 1], confidence)
show_distance(img, box, screen_center_x, screen_center_y)
break
cv2.imshow('Output', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
GitHub上的问题部分:
https://github.com/OlegQm/coco_objects_recognition/tree/main/stackoverflow_test
截图(概括:问题是绿色圆圈几乎从来不在绿色框内,尽管它应该在。2红色圆圈是窗口的中心。
The object on the left side
The object on the middle
The object on the right side
我试过更改OpenCV版本并检查数学。至少知道这个问题是由任何技术问题或我的错误引起的会很有趣,但解决方案是理想的。
1条答案
按热度按时间gmol16391#
我的错误是我误解了边界框的坐标。
您需要更换:
使用: