CV2不保存图像
该代码应该保存我使用检测到的对象的坐标设置的 roi。这部分没有发现错误,但它不保存图像。
path = "C:\HelmetDetection"
dt = str(datetime.now().strftime("%Y%m%d-%H:%M:%S"))
overlapping = bool()
instance = None
def check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2)
if instance == "ins1":
global overlapping
overlapping = True
else:
overlapping = False
def save_image(roi):
status = cv2.imwrite(os.path.join(path, dt + '.jpg'), roi)
print(status)
def check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
global instance
if x1 < x2 and y1 > y2 and trc1 > trc2 and blc1 < blc2:
instance = "ins1"
if label == "motorcycle":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
mcoords = []
mcoords.append((x, y, x + w, y + h))
if len(mcoords) == 1:
x1, y1, trc1, blc1 = x, y, x + w, y + h
else:
x1, y1, trc1, blc1 = mcoords[0]
if label == "bicycle":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
x1, y1, trc1, blc1 = x, y, x + w, y + h
bcoords = []
bcoords.append((x, y, x + w, y + h))
if len(bcoords) == 1:
x1, y1, trc1, blc1 = x, y, x + w, y + h
else:
x1, y1, trc1, blc1 = bcoords[0]
if label == "person":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
hcoords = []
hcoords.append((x, y, x + w, y + h))
if len(hcoords) == 1:
x2, y2, trc2, blc2 = x, y, x + w, y + h
else:
x2, y2, trc2, blc2 = hcoords[0]
if 'x1' and 'y1' and 'trc1' and 'blc1' and 'x2' and 'y2' and 'trc2' and 'blc2' in locals():
check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2)
!!!
if overlapping == True:
check_instance()
if instance == "ins1":
if (y2 or blc2 or x1 or trc1) > 100:
roi = img[y2 - 100:blc2 + 100, x1 - 100:trc1 + 100]
save_image(roi)
!!!
它返回 False,据我所知,这意味着图像无法保存。我能得到一些易于理解的解决方案吗?
A/N:这些只是片段,这是一个漫长的项目,所以请告诉我您是否需要有关所使用代码的更多信息。
The code is supposed to save the roi I have set using the coordinates of detected objects. No errors were found on this part, but it doesnt save the image.
path = "C:\HelmetDetection"
dt = str(datetime.now().strftime("%Y%m%d-%H:%M:%S"))
overlapping = bool()
instance = None
def check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2)
if instance == "ins1":
global overlapping
overlapping = True
else:
overlapping = False
def save_image(roi):
status = cv2.imwrite(os.path.join(path, dt + '.jpg'), roi)
print(status)
def check_instance(x1, y1, trc1, blc1, x2, y2, trc2, blc2):
global instance
if x1 < x2 and y1 > y2 and trc1 > trc2 and blc1 < blc2:
instance = "ins1"
if label == "motorcycle":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
mcoords = []
mcoords.append((x, y, x + w, y + h))
if len(mcoords) == 1:
x1, y1, trc1, blc1 = x, y, x + w, y + h
else:
x1, y1, trc1, blc1 = mcoords[0]
if label == "bicycle":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
x1, y1, trc1, blc1 = x, y, x + w, y + h
bcoords = []
bcoords.append((x, y, x + w, y + h))
if len(bcoords) == 1:
x1, y1, trc1, blc1 = x, y, x + w, y + h
else:
x1, y1, trc1, blc1 = bcoords[0]
if label == "person":
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0), 1)
cv2.putText(img, label + " " + f'{confidence * 100}%', (x, y + 20), font, 1, (0, 0, 0), 1)
hcoords = []
hcoords.append((x, y, x + w, y + h))
if len(hcoords) == 1:
x2, y2, trc2, blc2 = x, y, x + w, y + h
else:
x2, y2, trc2, blc2 = hcoords[0]
if 'x1' and 'y1' and 'trc1' and 'blc1' and 'x2' and 'y2' and 'trc2' and 'blc2' in locals():
check_if_overlapping(x1, y1, trc1, blc1, x2, y2, trc2, blc2)
!!!
if overlapping == True:
check_instance()
if instance == "ins1":
if (y2 or blc2 or x1 or trc1) > 100:
roi = img[y2 - 100:blc2 + 100, x1 - 100:trc1 + 100]
save_image(roi)
!!!
It returns False, which as I have read, means the image failed to save. Can I get some easily understandable solutions?
A/N: These are just snippets, it is kind of a long project, so just tell me if you need more information about the codes used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我总是喜欢做的事情是使用 open() 测试我的文件路径设置,因为它会引发更多有用的错误。
在你的情况下,它会抛出一个无效的路径错误,如果你开始消除奇怪的字符,这是由于日期/时间格式中的“:##:”造成的,
我建议将你的格式切换为不包含冒号
Something I always like to do is to test my filepath setup using open() as it throws much more useful errors.
In your case it throws an invalid path error, which if you start eliminating weird characters is due to the “:##:” in the date/time format
I would recommend switching your format to not contain colons