You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python红外图像相似度检测代码抛出ValueError问题求助

Python红外图像相似度检测代码抛出ValueError问题求助

我是编程新手,目前正在跟着身边的软件工程师同事,通过实际案例学习Python。我的目标是修改代码来实现红外图像的相似度检测,之前遇到的小问题大多都自己解决了,但这次碰到的报错实在搞不定,想请大家帮忙看看。

报错信息

ValueError                                Traceback (most recent call last)

c:\Users\xxxxx\Documents (This PC)\VSCode\projects\image_compare.py in line 46

44 img_path = os.path.join(path,img)

45 #test_img = cv2.imread(img_path)

---> 46 gnum = get_gnum(img)

47 base_image = []

48 base_image.append(img_path)

c:\Users\xxxxx\Documents (This PC)\VSCode\projects\image_compare.py in line 20, in get_gnum(path)

18 def get_gnum(path):

19     split_underline = path.split('_')[0]

---> 20     return int(split_underline)

ValueError: invalid literal for int() with base 10: '--ip=127.0.0.1'

完整代码(已修改路径保护隐私)

# %%

import numpy as np
import cv2
import os
import glob
import matplotlib.pyplot as plt
from sys import argv
from image_similarity_measures.quality_metrics import fsim, ssim

def sort_num(path):
    split_folder = path.split('\\')[-1]
    #print(split_folder)
    split_underline = split_folder.split('_')[-1]
    #print(split_ubderline)
    return int(split_underline[0:-4])

def get_gnum(path):
    split_underline = path.split('_')[0]
    return int(split_underline)

def compare(or_imgs, test_img):
    Use = False
    similarity = 0
    ssim_measures = {}
    test = cv2.imread(test_img)#,cv2.IMREAD_UNCHANGE)
    #test = test.astype(np.int64)
    #print(len(or_imgs))
    for or_img in or_imgs:
        original = cv2.imread(or_img)#, cv2.IMREAD_UNCHANGE)
        #original = original.astype(np.int64)
        #print(or_img, test_img)
        ssim_measures[or_img] = ssim(test, original)
    if max(ssim_measures.values()) < 0.94:
        or_imgs.append(test_img)
        Use = True
        print(test_img, 'score', max(ssim_measures.values()))
    return Use, or_imgs

path = '/yyy/xxxx_zz/Annotation_footage_1hz'

img = argv[1]
img_path = os.path.join(path,img)

#test_img = cv2.imread(img_path)

gnum = get_gnum(img)
base_image = []
base_image.append(img_path)

ssim_measures = {}

#fsim_measures = {}

#scale_percent = 100 # percent of original img size
#width = int(test_img.shape[1] * scale_percent / 100)
#height = int(test_img.shape[0] * scale_percent / 100)
#dim = (width, height)

#data_dir = 'data

comp_img_path = os.path.join(path,'%d_*.bmp' %gnum)
file_list = glob.glob(comp_img_path)
file_list.sort(key=sort_num)

#print(file_list[0:2])

for file in file_list:
    #img_path = os.path.join(data_dir, file)
    #print(file)
    #data_img = cv2.imread(file)
    Use, base_image = compare(base_image, file)
    print(len(base_image))

#resized_img = cv2.resize(data_img, dim, interpolation = cv2.INTER_AREA)
#fsim_measures[file]= fsim(test_img, resized_img)

def calc_closest_val(dict, checkMax):
    result = {}
    if (checkMax):
        closest = max(dict.values())
    else:
        closest = min(dict.values())
    for key, value in dict.items():
        print("The difference between ", key ," and the original image is : \n", value)
        if (value == closest):
            result[key] = closest
    print("The closest value: ", closest)
    print("######################################################################")
    return result

#ssim = calc_closest_val(ssim_measures, True)
#fsim = calc_closest_val(fsim_measures, True)

#print("The most similar according to SSIM: " , ssim)
#print("The most similar according to fsim: " , fsim)

# %%

我尝试过的解决方法

  • 切换到Conda的(base)环境运行代码
  • 给相关变量加/去引号
  • 将get_gnum函数里的int(split_underline)改成int(split_underline[0])
  • 搜索了很多相同报错的问题,但都没找到适配我这个场景的解决方案

我猜测问题可能出在gnum的定义或者get_gnum函数的逻辑上?也试着替换过img_path、path、img这些变量的组合,但还是没搞明白哪里出错了。

希望有大佬能帮我分析一下,谢谢大家的时间!


备注:内容来源于stack exchange,提问作者user22151836

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 11:18:12