imageai是一套开源免费的可用于图像智能识别的python应用包。好的模型可以识别图像中的对象。imageai图像预检目前实现了四种模型的算法支持,分别是SqueezeNet、ResNet、InceptionV3和DenseNet。不同的包对应不同大小的训练模型,准确率也不一致。SqueezeNet(文件大小:4.82MB,预测时间最短,准确度适中)MicrosoftResearch的ResNet50(文件大小:98MB,预测时间快,准确度高)GoogleBrain团队的InceptionV3(文件大小:91.6MB,预测时间更慢,更高)accuracy)DenseNet121byFacebookAIResearch(filesize:31.6MB,predictiontimeslowerpredictiontime,highestaccuracy)imageai不仅可以用于图像识别,还可以用于图形对象检测、视频对象预测和跟踪、自定义训练模型等。对机器学习不够深入的人非常友好,几行代码就可以实现一个AI应用。更深入的内容可以查看下方原文,简单了解如何在python本地服务中搭建图像识别应用。安装imageAI的依赖imageAI的依赖python(>=3.5.1)tensorflow(>=1.4.0)Numpy(>=1.13.1)SciPy(>=0.19.1)openCvpillowmatplotlibh5pykeras如果安装了pip3,可以直接运行pip3installtensorflownumpyscipyopencv-pythonpillowmatplotlibh5pykerasimageaiimageAI的训练模型imageAI支持使用4种不同的机器学习算法在ImageNet-1000数据集上训练,也支持在CoCo数据集上训练的物体检测。图像预检需要下载经过训练的模型。这里我们直接使用facebookAI的训练模型DenseNet121。废话不多说,上面的代码:fromimageai.PredictionimportImagePredictionimportos,jsonexecution_path=os.getcwd()#当前命令行执行路径prediction=ImagePrediction()#获取这个图像预测实例prediction.setModelTypeAsDenseNet()#facebookAI,这个根据下一步加载的模型文件建立对应的模型类型。其他还有setModelTypeAsSqueezeNet(常用)setModelTypeAsResNet(微软模型)和setModelTypeAsInceptionV3(谷歌模型)。path.join(execution_path,"DenseNet-BC-121-32.h5"))#加载模型文件,也就是刚刚下载的那个prediction.loadModel(prediction_speed="fast")#速度调整,如果你配置这个,需要用到准确率高的模型,否则会出现识别率预测降低的问题,probabilities=prediction.predictImage(os.path.join(execution_path,"test.jpg"),result_count=6)#对应的图像识别路径,以及对应的返回结果个数predictionDict={}foreachPrediction,eachProbabilityinzip(predictions,probabilities):predictionDict[eachPrediction]=eachProbabilityres=json.dumps(predictionDict)print(res)#输出结果虽然这里设置的是“快”,但实际上单核运行速度还是比较慢的。如果你的运行服务器是多核的,可以尝试并行计算,使用多核来提高效率。代码对应行都有相应注释,看一下打印结果:{"loudspeaker":33.06401073932648,"modem":26.803183555603027,"hard_disc":7.0777274668216705,"projector":4.804840311408043,"lighter":2.9418328776955605,"electric_fan":1.8662691116333008}返回的是key-value的json格式,key代表物品名称,value代表概率百分比值,最高为100,最低为0,分别代表准确识别和不可能。由于训练模型使用的是FacebookAI类型,标签名称也是英文的,对于中国用户可能需要一个翻译过程。如何用python翻译中英文,这里就不细说了,试试用pip安装googletrans,不过需要翻墙。好了,有了基本的识别逻辑,我们就可以搭建一个http服务,使用get请求接收图片文件名,然后让Python帮我们识别上传的图片里有什么。上面的代码:fromhttp.serverimportHTTPServer,BaseHTTPRequestHandlerimportio,shutil,urllibfromimageai.PredictionimportImagePredictionimportos,jsonclassMyHttpHandler(BaseHTTPRequestHandler):defdo_GET(self):name=""if'?'在self.path中:自我。queryString=urllib.parse.unquote(self.path.split('?',1)[1])#name=str(bytes(params['name'][0],'GBK'),'utf-8')params=urllib.parse.parse_qs(self.queryString)name=params["name"][0]if"name"inparamselseNoner_str=nameenc="UTF-8"encoded=''.join(r_str).encode(enc)ifname:execution_path=os.getcwd()ifexecution_path==''orexecution_path=='/':execution_path='/www-root/blog'#因为在网关模式下执行Python可能导致当前执行路径为'/'或为空时。prediction=ImagePrediction()prediction.setModelTypeAsDenseNet()#facebookAIprediction.setModelPath(os.path.join(execution_path,"DenseNet-BC-121-32.h5"))prediction.loadModel(prediction_speed="fast")预测,probabilities=prediction.predictImage(os.path.join(execution_path,"image/"+name),result_count=3)predictionDict={}foreachPrediction,eachProbabilityinzip(predictions,probabilities):predictionDict[eachPrediction]=eachProbabilityres=事件json.dumps(predictionDict)encoded=''.join(res).encode(enc)f=io.BytesIO()f.write(encoded)f.seek(0)self.send_response(200)self.send_header("内容类型","text/html;charset=%s"%enc)self.send_header("Content-Length",str(len(encoded)))self.end_headers()shutil.copyfileobj(f,self.wfile)Httpd=HTTPServer(('',8080),MyHttpHandler)print("Serverstartedon127.0.0.1,port8080....")httpd.serve_forever()在服务器上运行python3,你的脚本名.py就完美了它正在运行!curl-XGET'http://127.0.0.1:8080?name=IMG_8590.JPG'#response{"loudspeaker":33.06401073932648,"modem":26.803183555603027,"hard_disc":7.0777274668216705,"projector3101073932648","projector3101073932648":7.0777274668216705,"projector3101073932648":4.0777274668216705,"projector3401073932648"4.":2.9418328776955605,"electric_fan":1.8662691116333008}本文最后只介绍图像识别以及如何将其添加到pythonhttp服务器进行内部调用。其实你可以做的更多,比如图形物体检测、视频物体跟踪等。你还可以自定义训练模型,根据对应的4种算法对模型数据进行处理,打造自定义AI产品
