python截取摄像头的视频流为图片,输入模型进行物体识别

截取电脑摄像头的视频流,转化为帧并且输入给以inceptionV3训练的pd模型,输出结果。
可以修改阈值。

# -*- coding: cp936 -*-
"""
Author:xxxxxx
Date:2019-09-23
Discription:Read Camaro picture and save
"""

import cv2, os, time
import numpy as np
from multiprocessing import Process
import _thread
# coding: UTF-8
import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt

class CamaroCap(object):
    # 打开摄像头
    def __init__(self):
        # self.cap = cv2.VideoCapture(0)
        # self.cap = cv2.VideoCapture("rtsp://admin:[email protected]/cam/realmonitor?channel=1&subtype=0")  # 获取网络摄像机
        """
        self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480);
        """

        self.cap = cv2.VideoCapture(0)
    # 图片信息打印
    def get_image_info(self, image):
        print(type(image))
        print(image.shape)
        print(image.size)
        print(image.dtype)
        pixel_data = np.array(image)
        print(pixel_data)


    with tf.gfile.FastGFile('C:/Users/83543/Desktop/output_graph.pd', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')
    # 逐帧读取数据并保存图片到本地制定位置
    def Camaro_image(self):
        i = 0
        with tf.Session() as sess:
            softmax_tensor = sess.graph.get_tensor_by_name(
                'final_result:0')
            while (1):
                """
                ret:True或者False,代表有没有读取到图片
                frame:表示截取到一帧的图片
                """
                ret, frame = self.cap.read()
                ret, frame = self.cap.read()
                # print("打印图片信息")
                #self.get_image_info(frame)

                # 展示图片

                cv2.imshow('capture', frame)
                # 保存图片
                cv2.imwrite(r"C:\Users\83543\Desktop\videopic\\" + str(i) + ".jpg", frame)
                #

                res = ['beerbottle', 'glass', 'metal', 'waterbottle']
                 # 获取新模型最后的输出节点叫做final_result,可以从tensorboard中的graph中看到,其中名字后面的’:’之后接数字为EndPoints索引值(An operation allocates memory for its outputs, which are available on endpoints :0, :1, etc, and you can think of each of these endpoints as a Tensor.),通常情况下为0,因为大部分operation都只有一个输出。
                    # 遍历目录s
                    # for root, dirs, files in os.walk('C:/Users/83543/Desktop/vediopic'):  # 预测图片的位置

                image_data = tf.gfile.FastGFile("C:\\Users\83543\Desktop\\videopic\\"+str(i)+ ".jpg",'rb').read()  # Returns the contents of a file as a string.
                predictions = sess.run(softmax_tensor, {
                                'DecodeJpeg/contents:0': image_data})  # tensorboard中的graph中可以看到DecodeJpeg/contents是模型的输入变量名字
                predictions = np.squeeze(predictions)


                            # 展示图片
                            # img = plt.imread(image_path)#只能读png图,所以不能显示其他图片,训练非png图时把这段注释掉,他只是一个显示作用
                            # plt.imshow(img)
                            # plt.axis('off')
                            # plt.show()

                # top_k = predictions.argsort()[-2:][::-1]  # 概率最高的后2个,然后在倒排一下
                top_k = predictions.argsort()[-1:]
                for node_id in top_k:
                      score = predictions[node_id]
                      print('%s (score=%.5f)' % (res[node_id], score))
                print()
                i = i + 1
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break



# 创建一个图来存放google调整好的模型 inception_pretrain\classify_image_graph_def.pb
# 结果数组与C:\Users\admin\PycharmProjects\TensorFlowTestNew\TensorFlow\inception利用\output_labels.txt文件中的顺序要一致

if __name__ == '__main__':
    outmasages = CamaroCap()
    # 调用摄像头
    outmasages.Camaro_image()
    # 释放对象和销毁窗口
    outmasages.cap.release()
    cv2.destroyAllWindows()



部分结果图片:

python截取摄像头的视频流为图片,输入模型进行物体识别_第1张图片
训练集是白色玻璃罐,标签名字是glass。但是这个绿色的里面装了东西的也检测出来了。效果还可以。

你可能感兴趣的:(迁移学习)