猿问

python cv2 puttext - 显示变量时出错

我正在编写我的第一个代码之一,我想在图像上显示变量。从文本文件中读取变量。这是我的代码的一部分:


font=cv2.FONT_HERSHEY_SIMPLEX

fontScale=2

fontColor=(255,255,255)

lineType=2;


def line11():

    cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);

    cv2.putText(img, x,(443,320),fontScale, (255,255,255),lineType)

从 tex 文件中读取值 x:


with open('US1.txt') as f1, open('US2.txt') as f2: 

    for x, y in zip(f1,f2):

        x = x.strip()

        y = y.strip()

        print("{0} {1}".format(x, y))

不幸的是,我得到了错误:


TypeError                                 

Traceback (most recent call last)

<ipython-input-2-bc1d3b9f83c2> in <module>

    130 

    131         if (float(x) <=0.5):

--> 132             line11();

    133 

    134         elif (0.5< float(x)<=1):


<ipython-input-2-bc1d3b9f83c2> in line11()

     16     cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);

     17 

---> 18     cv2.putText(img, x, (443,350),fontScale, (255,255,255),lineType)

     19 

     20 def line12():


TypeError: must be real number, not tuple

我找不到解决方案。我尝试了很多选择(即x的清型),现在我很无助。有人可以向我解释吗?谢谢!


达令说
浏览 274回答 2
2回答

qq_遁去的一_1

你错过了这个论点。试试这个:fontfont=cv2.FONT_HERSHEY_SIMPLEXfontScale=2fontColor=(255,255,255)lineType=cv2.line_AAorg=(443,320)text = str(x)cv2.putText(img, text,org,font,fontScale,fontColor,lineType)

繁星点点滴滴

我想这个错误是因为你忘了在putText函数上提到字体。它得到的元组是(255,255,255),而它期待的是flotScale。cv2.put文本(图像、文本、组织、字体、字体比例、颜色[、粗细[、线型[、底部左图]]])尝试: cv2.put文本(img, x,(443,320),字体, 字体缩放, 字体颜色, 行型)
随时随地看视频慕课网APP

相关分类

Python
我要回答