python - Python:TypeError:需要一个整数(获取类型模块)
问题描述
我有小代码来读取文件夹上的所有图像并将它们交替发送到当前 API。但是这段代码还没有工作,如何解决这个问题。谢谢你。
import cv2
import os
import json
import requests
import time
vehicle_count = [0]
current_path = os.getcwd() #+ "/"
file = '/image_folder/image' + str(len(vehicle_count)) + ".png"
path = '/home/username/Documents/path/to/image_folder%s' % file
temp_file = current_path + file
result = []
def send_image(source_image):
cv2.imread(temp_file , source_image)
vehicle_count.insert(0, 1)
with open(path, 'rb') as fp:
response = requests.post(
'https://url_to_API/',
files=dict(upload=fp),
headers={'Authorization': 'Token ' + 'MY_API'})
result.append(response.json())
print(json.dumps(result, indent=2));
with open('data.json', 'w') as outfile:
json.dump(result, outfile)
os.remove("%s" %temp_file)
这是错误回溯:
Traceback (most recent call last):
File "plate_detection_main.py", line 234, in <module>
object_detection_function()
File "plate_detection_main.py", line 133, in object_detection_function
line_thickness=1,
File "/home/smartron01/Documents/tf-object-counting/vehicle_counting_tensorflow/utils/visualization_utils.py", line 515, in visualize_boxes_and_labels_on_image_array
use_normalized_coordinates=use_normalized_coordinates)
File "/home/smartron01/Documents/tf-object-counting/vehicle_counting_tensorflow/utils/visualization_utils.py", line 128, in draw_bounding_box_on_image_array
use_normalized_coordinates)
File "/home/smartron01/Documents/tf-object-counting/vehicle_counting_tensorflow/utils/visualization_utils.py", line 182, in draw_bounding_box_on_image
predicted_direction, predicted_speed, is_vehicle_detected, update_csv = speed_prediction.predict_speed(top, bottom, right, left, current_frame_number, detected_vehicle_image, ROI_POSITION)
File "/home/smartron01/Documents/tf-object-counting/vehicle_counting_tensorflow/utils/speed_and_direction_prediction_module/speed_prediction.py", line 46, in predict_speed
image_api_sender.send_image(image_saver) # send image to platerecognizer.com
File "/home/smartron01/Documents/tf-object-counting/vehicle_counting_tensorflow/utils/image_utils/image_api_sender.py", line 15, in send_image
cv2.imread(temp_file , source_image)
TypeError: an integer is required (got type module)
解决方案
问题出在这里:cv2.imread(temp_file , source_image)
,您将两个参数传递给imread()
只需要一个的函数。尝试:cv2.imread(temp_file)
假设 temp_file 是您要加载的图像文件的正确路径。
注意:您可以传递给 imread(path, flag) 函数的第二个参数是 flag,它指定读取图像的方式。它的默认值为 cv2.IMREAD_COLOR
推荐阅读
- python - 如何将数据传感器发送到托管 Web 应用程序
- r - 如何为循环中生成的图生成唯一名称,以免它们最终相互覆盖?
- python - 准确率在 epoch 中间突然下降了 10%
- ros - MoveIt:入门。无法配置catkin工作空间
- python - 将元组添加到熊猫数据库 python
- powershell - 如果在特定组中,则运行 powershell 脚本
- r - 我可以在 Xaringan 中结合磁贴视图和演示者模式吗?
- xss - 在这种情况下是否可以使用 XSS 负载?
- reactjs - React:可加载组件增加 Cumulative Layout Shift (CLS)
- python - 如何更改networkx图中的节点位置?