如何解决这个python错误?
回溯(最近一次呼叫最后一次): 文件“final.py”,第21行,在 lpd=许可证检测器(图像) TypeError:“模块”对象不可调用 我实际上在两个文件中运行这个程序,第一个文件由这个类函数组成如何解决这个python错误?,python,Python,回溯(最近一次呼叫最后一次): 文件“final.py”,第21行,在 lpd=许可证检测器(图像) TypeError:“模块”对象不可调用 我实际上在两个文件中运行这个程序,第一个文件由这个类函数组成 from collections import namedtuple from skimage.filters import threshold_local from skimage import segmentation from skimage import measure from im
from collections import namedtuple
from skimage.filters import threshold_local
from skimage import segmentation
from skimage import measure
from imutils import perspective
import numpy as np
import imutils
import cv2
LicensePlate = namedtuple("LicensePlateRegion", ["success", "plate", "thresh", "candidates"])
class LicensePlateDetector:
print("entered class")
def __init__(self, image, minPlateW=60, minPlateH=20, numChars=7, minCharW=40):
self.image = image
self.minPlateW = minPlateW
self.minPlateH = minPlateH
self.numChars = numChars
self.minCharW = minCharW
def detect(self):
lpRegions = self.detectPlates()
for lpRegion in lpRegions:
lp = self.detectCharacterCandidates(lpRegion)
if lp.success:
yield (lp, lpRegion)
def detectCharacterCandidates(self, region):
plate = perspective.four_point_transform(self.image, region)
cv2.imshow("Perspective Transform", imutils.resize(plate, width=400))
V = cv2.split(cv2.cvtColor(plate, cv2.COLOR_BGR2HSV))[2]
T = threshold_local(V, 29, offset=15, method="gaussian")
thresh = (V > T).astype("uint8") * 255
thresh = cv2.bitwise_not(thresh)
plate = imutils.resize(plate, width=400)
thresh = imutils.resize(thresh, width=400)
cv2.imshow("Thresh", thresh)
labels = measure.label(thresh, neighbors=8, background=0)
charCandidates = np.zeros(thresh.shape, dtype="uint8")
for label in np.unique(labels):
if label == 0:
continue
labelMask = np.zeros(thresh.shape, dtype="uint8")
labelMask[labels == label] = 255
cnts = cv2.findContours(labelMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
(boxX, boxY, boxW, boxH) = cv2.boundingRect(c)
aspectRatio = boxW / float(boxH)
solidity = cv2.contourArea(c) / float(boxW * boxH)
heightRatio = boxH / float(plate.shape[0])
keepAspectRatio = aspectRatio < 1.0
keepSolidity = solidity > 0.15
keepHeight = heightRatio > 0.4 and heightRatio < 0.95
if keepAspectRatio and keepSolidity and keepHeight:
hull = cv2.convexHull(c)
cv2.drawContours(charCandidates, [hull], -1, 255, -1)
charCandidates = segmentation.clear_border(charCandidates)
return LicensePlate(success=True, plate=plate, thresh=thresh,candidates=charCandidates)
我写了这些,然后使用cmd运行名为python final.py-I California1.jpg的命令。其中final.py是由代码的第二部分组成的文件名,即argparse one和California one是由数字板组成的图像,Python不是Java。如果您想要访问一个类,您需要直接导入它,或者通过您导入的模块引用它。因此:
import LicensePlateDetector
...
lpd = LicensePlateDetector.LicensePlateDetector(image)
或
这就是为什么PEP8建议将文件命名为带有下划线的
小写字母。py
类应该是LicensePlateDetector(对象):@JohnJosephFernandes不应该。如果像LicensePlateDetector(对象)那样编写,则会出错。
import LicensePlateDetector
...
lpd = LicensePlateDetector.LicensePlateDetector(image)
from LicensePlateDetector import LicensePlateDetector
...
lpd = LicensePlateDetector(image)