回溯(最近通话最近): 在第21行的文件“ final.py”中 lpd = LicensePlateDetector(图片) TypeError:“模块”对象不可调用
我实际上是在两个文件中运行该程序,第一个是由此类函数组成的
/posts//comments
/posts/"any-string"/comments
我在其中使用argparse的第二个文件
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一个,而加利福尼亚一个则是由车号牌组成的图像
答案 0 :(得分:1)
Python不是Java。如果要访问一个类,则需要直接将其导入,或通过已导入的模块对其进行引用。所以:
import LicensePlateDetector
...
lpd = LicensePlateDetector.LicensePlateDetector(image)
或
from LicensePlateDetector import LicensePlateDetector
...
lpd = LicensePlateDetector(image)
这是PEP8建议将文件命名为lower_case_with_underscore.py
的原因之一。