基于Tensorflow实现CNN物体识别系统

这篇博客详细介绍了如何基于Tensorflow实现一个CNN物体识别系统,包括获取数据集、数据预处理、模型构建和GUI界面设计。作者通过爬虫从百度获取数据,并对数据进行筛选和规格统一,然后构建深度学习模型,最后实现了一个用户界面。
摘要由CSDN通过智能技术生成

基于Tensorflow实现CNN物体识别系统

目录

1.获取数据集
2.数据预处理
3.建立模型
4.GUI界面

一、获取数据集

该项目所使用的数据集通过爬虫从百度中获取数据

import requests
import os
import urllib.parse
import json
import jsonpath

header = {
   
    'User-Agent': 'Mozilla/5.0(Macintosh;Inter Mac OS X 10_13_3) AppleWebkit/537.36 (KHTML,like Gecko)'
                  'Chrom/65.0.3325.162 Safari/537.36'
}

# https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%8F%AF%E7%88%B1%E5%A4%B4%E5%83%8F&cl=2&word=%E5%8F%AF%E7%88%B1%E5%A4%B4%E5%83%8F&pn=30&rn=30
#https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E7%8C%AB&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word=%E7%8C%AB&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1581575909562=
# url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={}&word={}&pn={}&rn=30'
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word={}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn={}&rn=30'
# queryWord字段可自行更改,想搜什么写什么
queryWord = '自行车'
queryWords = urllib.parse.quote(queryWord)
word = queryWords
# print(queryWords)
num = 1
for pn in range(0, 2000, 30):
    try:
        urls = url.format(queryWords, word, pn)
        response = requests.get(urls, headers=header).text
        html = json.loads(response)
        photos = jsonpath.jsonpath(html, '$..thumbURL')

    except:
        pass


    # print(html)
    # photos = jsonpath.jsonpath(html,'$..thumbURL')
    # print(photos)
    def mkdir(path):

        folder = os.path.exists(path)

        if not folder:  # 判断是否存在文件夹如果不存在则创建为文件夹
            os.mkdir(path)  # mkdir 创建文件时如果路径不存在会创建这个路径
            print
            "---  new folder...  ---"
            print
            "---  OK  ---"

        else:
            print
            "---  There is this folder!  ---"


    path = 'img/%s' % queryWord  # 自行更改存储地
    mkdir(path)
    if type(photos) is not bool:
        for i in photos:
            try:
                a = requests.get(i, headers=header)
                with open('{}/{}.jpg'.format(path, num), 'wb')as f:
                    print("正在下载第%s张图片" % num)
                    f.write(a.content)
                    num += 1
            except:
                pass
    else:
        pn += 1


二、数据预处理

自己爬取的图片有很多图片并不是该分类的东西所以剔除对训练有影响的数据。
提出后的数据,图片的规格不同所以统一规格

from PIL import Image
import os
def process_image_channels(image_path):
#将4通道,A通道的统一成三通道的图像;
#  process the 4 channels .png
    print("正在转换图片通道数。。。。。。。")
    for img_name in os.listdir(image_path):
        img_path = image_path + "/" + img_name
        # 获取该图片全称
        image = Image.open(img_path)              # 打开特定一张图片
        image = image.resize((64, 64))            # 设置需要转换的图片大小
        if image.mode == 'RGBA':
            r, g, b, a = image.split()
            image = Image.merge("RGB", (r, g, b))
            os.remove(img_path)
            # 用新生成的3通道的图片代替原来的;
            image.save(img_path)
            print("这是个四通道的,处理完了!")
        #  process the 1 channel image
        elif image.mode != 'RGB':
            image = image.convert("RGBA")
            r, g, b, a = image.split()
            image = Image.merge("RGB", (r, g, b))
            os.remove(img_path)
            image.save(img_path)
            print("这是个A通道的,处理完了!")
    print("-----------通道数变换完毕-----------")

def image_reshape(image_path, size):
    i = 1
    print("正在统一图片尺寸。。。。。。。")
    for img_name in os.listdir(image_path):
        img_path = image_path + "/" + img_name    # 获取该图片全称
        image = Image.open(img_path)              # 打开特定一张图片
        image = image.resize(size)            # 设置需要转换的图片大小
        os.remove(img_path)
        image.save(img_path)
        print("-----------尺寸统一完毕-----------",i)
        i += 1

image_path = 'img/飞机'
process_image_channels(image_path)
image_reshape(image_path, (100, 100))

后续还得将图片转化为神经网络能够识别的数据

import numpy as np
import os
import matplotlib.pyplot as plt

train_path = "img/train/"  # 图片存储位置
test_path = "img/test/"  # 图片存储位置

# 获取数据
train_x_path = []
train_y_data = []

test_x_path = []
test_y_data = []


def img_data(path, x, y):
    # 读取图片路径以及生成标签数据
    for item in os.listdir(path):
        file_path = path+item
        x.append(file_path)
        if "T" in item:
            y.append([1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
        elif "r" in item:
            y.append([0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
        elif "w" in item:
            y.append([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])
        elif "m" in item:
            y.append([0, 0, 0, 1, 0, 0, 0, 0, 0, 0])
        elif "d" in item:
            y.append([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])
        elif "cat" in item:
            y.append([0, 0, 0, 0, 0, 1, 0, 0, 0, 0])
        elif "s" in item:
            y.append([0, 0, 0, 0, 0, 0, 1, 0, 0, 0])
        elif "watermelon" in item:
            y.append([0, 0, 0, 0, 0, 0, 0, 1, 0, 0])
        elif "C" in item:
            y.append([0, 0, 0, 0, 0, 0, 0, 0, 1, 0])
        else:
            y.append([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

    # 转化为矩阵
    x_path = np.array(x)
    y_data = np.array(y)

    # 乱序原始数据
    np.random.seed(100)
    order = np.random.permutation(len(y_data))
    x_path = x_path[order]
    y_data = y_data[order]

    <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值