首页 > 解决方案 > 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)

标签: python

解决方案


问题出在这里:cv2.imread(temp_file , source_image),您将两个参数传递给imread()只需要一个的函数。尝试:cv2.imread(temp_file)假设 temp_file 是您要加载的图像文件的正确路径。

注意:您可以传递给 imread(path, flag) 函数的第二个参数是 flag,它指定读取图像的方式。它的默认值为 cv2.IMREAD_COLOR


推荐阅读