暑假实习,一位做算法的老师让我们一行人将摄像头拍取的车辆照片按车型分类保存。
示例如下:
这样的图片共有上万张,且有多个文件夹,人工打开图片、放大,再识别(如果不清楚车辆标志,还需上网查找),并Ctrl+C、Ctrl+V将其保存在相应文件夹下,这着实让人感到无聊与繁琐。
因此,我就萌发了用熟知的python写个脚本,自动化完成工作。
上面想法很好,但是实际行动起来我还是遇到了很多问题。
1.环境配置
编译环境:Python3.6,Spyder
依赖模块:shelve,PIL,shutil
2.申请API
找到并下载车型识别Python的SDK
车型识别的示例:
"""读取图片"""defget_file_content(filePath):withopen(filePath,'rb')asfp:returnfp.read()image=get_file_content('example.jpg')"""调用车辆识别"""client.carDetect(image);"""如果有可选参数"""options={}options["top_num"]=3options["baike_num"]=5"""带参数调用车辆识别"""client.carDetect(image,options)3.指定目录下所有车型的获得
对API调用返回JSON数据的清洗,提取所需要的信息(取第一个)
car_info=client.carDetect(img)try:car_color=car_info['color_result']except:car_color='无法识别'try:car_name=car_info['result'][0]['name']car_score=car_info['result'][0]['score']car_year=car_info['result'][0]['year']except:car_name='非车类'car_score=1car_year='无年份信息'car_result=[car_color,car_name,car_score,car_year,file]获取指定目录下的所有车辆照片的车型
path='..'img_path=path+'\\car_img'#调用API获取指定目录下所有文件的车型,并将数据保存m_files=os.listdir(img_path)foriinrange(len(m_files)):results=[]files_path=img_path+'\\'+m_files[i]imgs=os.listdir(files_path)forjinrange(len(imgs)):#out_path,img=img_cut(m_files[i],imgs[j])result=get_info(out_path,img)results.append(result)data_path=path+'\\'+'data'+'\\'+m_files[i]shelf_save(results,data_path)实际操作中,发现有些图片识别不出来,便裁剪一下,保留下半部分,竟然发现它能识别了。因此,在上传图片时首先对图片进行了裁剪。
#图片裁剪defimg_cut(file,img):img_read=Image.open(path+'\\car_img\\'+file+'\\'+img)a=[0,1300,3310,2600]box=(a)roi=img_read.crop(box)out_path=path+'\\图片处理\\'+fileifnotos.path.exists(out_path):os.mkdir(out_path)roi.save(out_path+'\\'+img)returnout_path,img我这里使用了shelve模块将每个文件夹数据进行保存与调用
defshelf_load(path):shelfFile=shelve.open(path)results=shelfFile['results']shelfFile.close()returnresultsdefshelf_save(results,path):shelfFile=shelve.open(path)shelfFile['results']=resultsshelfFile.close()4.根据车型分类建立文件夹
话不多说,直接上代码
由于调用次数限制,我跑了480张图片,仅有几张无法识别,识别率还可以。至于准确率,我简单翻看了一些目录下的照片,虽然有个别车型识别错误,但大多还可以的。这里仅展示已经自动分类好的文件。