python - How to draw bounding boxes on ROI using opencv
问题描述
And these are the key-value pairs in a text file:
camera: LG G3 D855
reading: 05501
position: 843 1492 572 119
digit 1: 855 1513 54 98
digit 2: 971 1512 61 100
digit 3: 1092 1512 61 100
digit 4: 1207 1501 64 99
digit 5: 1341 1499 38 97
I need to draw bounding boxes around digits. This is my code:
for i in image_list:
img = cv2.imread(os.path.join(image_dir,i))
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
matching = [s for s in text_files if i.replace(".jpg","") in s]
myvars = {}
with open(str(matching[0])) as myfile:
for line in myfile:
print(line)
name, var = line.partition(":")[::2]
myvars[name.strip()] = var
digits1 = myvars["digit 1"].split(" ")
print(digits1[1])
pts1 = (digits1[1], digits1[2])
pts2 = (digits1[1]+digits1[3], digits1[2]+digits1[4])
cv2.rectangle(img, pts1 , pts2 , (255,0,0), 2)
cv2.imshow("lalala", img)
k = cv2.waitKey(0) # 0==wait forever
This is giving this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-8ed670a7caaf> in <module>
26 pts1 = (digits1[1], digits1[2])
27 pts2 = (digits1[1]+digits1[3], digits1[2]+digits1[4])
---> 28 cv2.rectangle(img, pts1 , pts2 , 0, 2)
29 cv2.imshow("lalala", img)
30 k = cv2.waitKey(0) # 0==wait forever
TypeError: an integer is required (got type tuple)
How do I resolve this?
解决方案
拆分字符串时,在输出中您将得到一个字符串,但对于rectangle
函数,您需要将这些字符串转换为int
. 所以你在这些方面有一些错误:
digits1 = myvars["digit 1"].split(" ")
print(digits1[1])
pts1 = (digits1[1], digits1[2]) // a mistake happened in here
pts2 = (digits1[1]+digits1[3], digits1[2]+digits1[4]) // a mistake happened in here
您应该将这些字符串转换为 int,如下所示:
pts1 = (int(digits1[1]), int(digits1[2]))
pts2 = (int(digits1[1])+int(digits1[3]), int(digits1[2])+int(digits1[4]))
推荐阅读
- windows - 网站登录蝙蝠脚本未通过 Windows 任务计划程序运行
- c++ - 在地图的基础上存储字符串内容
C++ - context-info - 从 asp 应用程序连接到 SQL db 时设置 context_info
- asp.net-core - 找不到 ASP.NET Core ODataController
- python - 我在 Django 中使用 LoginRequiredMixin。如果我在搜索栏中输入 url,它会在未登录的情况下显示页面
- python - 如何将 Python 中的列表写入 NetCDF 文件?
- android - Google Play 封闭式测试安装计数
- apache - 当请求被重定向到 index.php 时,干净的 url 如何保留在您的地址栏中?
- javascript - 如何在谷歌地图的新功能中添加标记?
- haskell - 使用 GHC 9.0.1 的神秘 Unicode 输出