我正在編寫我的第一個代碼之一,我想在圖像上顯示變量。從文本文件中讀取變量。這是我的代碼的一部分:font=cv2.FONT_HERSHEY_SIMPLEXfontScale=2fontColor=(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的清型),現在我很無助。有人可以向我解釋嗎?謝謝!
2 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
你錯過了這個論點。試試這個:font
font=cv2.FONT_HERSHEY_SIMPLEX
fontScale=2
fontColor=(255,255,255)
lineType=cv2.line_AA
org=(443,320)
text = str(x)
cv2.putText(img, text,org,font,fontScale,fontColor,lineType)

繁星點點滴滴
TA貢獻1803條經驗 獲得超3個贊
我想這個錯誤是因為你忘了在putText函數上提到字體。它得到的元組是(255,255,255),而它期待的是flotScale。
cv2.put文本(圖像、文本、組織、字體、字體比例、顏色[、粗細[、線型[、底部左圖]]])
嘗試: cv2.put文本(img, x,(443,320),字體, 字體縮放, 字體顏色, 行型)
添加回答
舉報
0/150
提交
取消