HOG手写数字识别不起作用

如何解决HOG手写数字识别不起作用

我正在读一本OpenCV书中有关手写数字识别的一章,尽管我仔细阅读了一下,但我认为一切都得到了正确处理,但出现了错误public async Task<TestTable[]> GetAllEmployees() { IQueryable<TestTable> query = _context.TestTable; return await query.ToArrayAsync(); } 。我尝试用Google搜索答案,似乎很多其他人遇到了非常相似的问题,但没有提供真正的答案。

任何人都可以阐明为什么此Expected 2D array,got 1D array instead方法不返回2D数组吗?我正在阅读一些文档,显然它默认情况下会返回一个平面1D数组,所以我不知道为什么这个feature.hog()方法抱怨为什么期望使用2D数组。再说一遍,我想读的这本书是我想在2015年发行的,所以也许有什么变化吗?

这是我要运行的文件:

classify.py

model.predict()

这是为此编写的自定义生猪模块:

hog.py

# -*- coding: utf-8 -*-
"""
Created on Tue Nov  3 13:01:39 2020

@author: User
"""


from __future__ import print_function
from sklearn.externals import joblib
from pyimagesearch.hog import HOG
from pyimagesearch import dataset
import argparse
import mahotas
import cv2

ap = argparse.ArgumentParser()
ap.add_argument('-m','--model',required=True,help='Path to model')
ap.add_argument('-i','--image',help='Path to image')
args=vars(ap.parse_args())

model = joblib.load(args['model'])

hog = HOG(orientations=18,pixelsPerCell=(10,10),cellsPerBlock=(1,1),normalize=True)

image = cv2.imread(args["image"])
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray,(5,5),0)
edged = cv2.Canny(blurred,30,150)
(_,cnts,_) = cv2.findContours(edged.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted([(c,cv2.boundingRect(c)[0]) for c in cnts],key=lambda x: x[1])

for (c,_) in cnts:
    (x,y,w,h) = cv2.boundingRect(c)
    
    if w >= 7 and h>= 20:
        roi = gray[y:y+h,x:x+w]
        thresh = roi.copy()
        T = mahotas.thresholding.otsu(roi)
        thresh[thresh > T] = 255
        thresh = cv2.bitwise_not(thresh)
        
        thresh = dataset.deskew(thresh,72)
        thresh = dataset.center_extent(thresh,(72,72))
        
        cv2.imshow("thresh",thresh)
        
        hist = hog.describe(thresh)
        digit = model.predict(hist)[0] #this is where it errors
        print("I think that number is: {}".format(digit))
        
        cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1)
        cv2.putText(image,str(digit),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,1.2,2)
        cv2.imshow("image",image)
        cv2.waitKey(0)

这是为此产生“训练模型”的原因:

train.py

# -*- coding: utf-8 -*-
"""
Created on Tue Nov  3 11:22:38 2020

@author: User
"""

from skimage import feature

class HOG:
    def __init__(self,orientations=9,pixelsPerCell=(14,14),normalize=False):
        self.orientations = orientations
        self.pixelsPerCell = pixelsPerCell
        self.cellsPerBlock = cellsPerBlock
        self.normalize = normalize
    
    def describe(self,image):
        '''
        (2017-11-28) Update for skimage: In  scikit-image==0.12,the  
        normalise  parameter has been updated to  transform_sqrt . The  
        transform_sqrt  performs the exact same operation,only with a 
        different name. If you’re using an older version of  scikit-image  
        (again,before the v0.12 release),then you’ll want to change 
        transform_sqrt  to  normalise . In  scikit-image==0.15  the default 
        value of  block_norm="L1"  has been deprecated and changed to  
        block_norm="L2-Hys" . Therefore,for this lesson we’ll explicitly 
        specify  block_norm="L1" . Doing this will avoid it switching to  
        "L2-Hys"  with version updates without us knowing (and yielding 
        incorrect car logo identification results). You can read about L1 and 
        L2 norms here:
        https://gurus.pyimagesearch.com/lesson-sample-histogram-of-oriented-gradients-and-car-logo-recognition/#tour_modal
        '''
        hist = feature.hog(image,orientations=self.orientations,pixels_per_cell=self.pixelsPerCell,cells_per_block=self.cellsPerBlock,transform_sqrt =self.normalize,block_norm="L1")
        return hist

dataset.py

# -*- coding: utf-8 -*-
"""
Created on Tue Nov  3 11:57:26 2020

@author: User
"""

from sklearn.externals import joblib
from sklearn.svm import LinearSVC
from pyimagesearch.hog import HOG
from pyimagesearch import dataset
import argparse

ap = argparse.ArgumentParser()
ap.add_argument('-d','--dataset',help='Path to dataset')
ap.add_argument('-m',help='path to where model will be stored')
args=vars(ap.parse_args())

(digits,target) = dataset.load_digits(args['dataset'])

data = []

hog = HOG(orientations=9,normalize=True)

for image in digits:
    image = dataset.deskew(image,20)
    image = dataset.center_extent(image,(20,20))
    
    hist = hog.describe(image)
    data.append(hist)

model = LinearSVC(random_state=42)
model.fit(data,target)

joblib.dump(model,args['model'])

,如果需要的话,请使用此自定义# -*- coding: utf-8 -*- """ Created on Tue Nov 3 11:35:04 2020 @author: User """ from . import imutils import numpy as np import mahotas import cv2 def load_digits(datasetPath): data = np.genfromtxt(datasetPath,delimiter=',',dtype='uint8') target = data[:,0] data = data[:,1:].reshape(data.shape[0],28,28) return (data,target) def deskew(image,width): (h,w) = image.shape[:2] moments = cv2.moments(image) skew = moments['mu11'] / moments['mu02'] M = np.float32([ [1,skew,-0.5 * w * skew],[0,1,0]]) image = cv2.warpAffine(image,M,(w,h),flags = cv2.WARP_INVERSE_MAP | cv2.INTER_LINEAR) image = imutils.resize(image,width=width) return image def center_extent(image,size): (eW,eH) = size if image.shape[1] > image.shape[0]: image = imutils.resize(image,width=eW) else: image = imutils.resize(image,height=eH) extent = np.zeros((eH,eW),dtype = 'uint8') offsetX = (eW - image.shape[1]) // 2 offsetY = (eH - image.shape[0]) // 2 extent[offsetY:offsetY + image.shape[0],offsetX:offsetX + image.shape[1]] = image CM = mahotas.center_of_mass(extent) (cY,cX) = np.round(CM).astype('int32') (dX,dY) = ((size[0] // 2) - cX,(size[1] // 2) - cY) M = np.float32([[1,dX],dY]]) extent = cv2.warpAffine(extent,size) return extent 模块

imutils.py

imutils

并且我正在使用此数据found here (the train.csv file),并且通过此脚本将其减少为5000行:

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 16:27:16 2020

@author: User
"""

import numpy as np
import cv2

def translate(image,x,y):
    M = np.float32([[1,x],y]])
    shifted = cv2.warpAffine(image,(image.shape[1],image.shape[0]))
    return shifted

def rotate(image,angle,center=None,scale=1.0):
    (h,w) = image.shape[:2]
    if not center:
        center = (w // 2,h // 2)
    M = cv2.getRotationMatrix2D(center,scale)
    rotated = cv2.warpAffine(image,h))
    return rotated

def resize(image,width=None,height=None,inter=cv2.INTER_AREA):
    dim = None
    (h,w) = image.shape[:2]
    
    if width is None and height is None:
        return image
    
    if width is None:
        r = height / float(h)
        dim = (int(w*r),height)
    
    else:
        r = width / float(w)
        dim = (width,int(h*r))
    
    resized = cv2.resize(image,dim,interpolation = inter)
    return resized

解决方法

我已经想通了一段时间,但现在有机会发帖,所以我想分享我的发现。

我认为当我试图“重塑”数组时,我实际上是在重塑错误的数组,这就是为什么它一直给我一个错误。

所以我想将我拥有的一维数组转换为二维数组,我选择了这一行:digit = model.predict(hist)[0]

并将其更改为:digit = model.predict(hist.reshape(1,-1))[0]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res