简体   繁体   English

Python:对图像中的对象进行分类

[英]Python: classify objects in images

camera = webcam; % Connect to the camera
nnet = alexnet;  % Load the neural net

while true   
    picture = camera.snapshot;              % Take a picture    
    picture = imresize(picture,[227,227]);  % Resize the picture

    label = classify(nnet, picture);        % Classify the picture

    image(picture);     % Show the picture
    title(char(label)); % Show the label
    drawnow;   
end

I found this matlab code in the internet. 我在互联网上找到了这个matlab代码。 It displays a window with the picture from a webcam and very quickly also names the things in the picture ("keyboard","bootle","pencil","clock"...). 它显示一个窗口,其中包含来自网络摄像头的图片,并且非常快速地命名图片中的内容(“键盘”,“布特”,“铅笔”,“时钟”......)。 I want to do that in python. 我想在python中做到这一点。 So far I have this: 到目前为止我有这个:

import cv2
import sys
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
video_capture = cv2.VideoCapture(0)

while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

This is alreay very similar, but only detecting faces. 这是非常相似的,但只检测面部。 The matlab code uses alexnet. matlab代码使用alexnet。 I guess this is a pre-trained network based on imagenet data ( http://www.image-net.org/ ). 我想这是一个基于imagenet数据的预训练网络( http://www.image-net.org/ )。 But it is no longer available. 但它已不再可用。 How would I do this in python? 我怎么能在python中这样做?

(There has been a similar question here, but it is 4 yrs. old and I think there are newer techniques now). (这里有一个类似的问题,但它已经过了4年了,我认为现在有更新的技术)。

With the "tensorflow" package and the pre-trained network "vgg16", the solution is quite easy. 使用“tensorflow”软件包和预先培训的网络“vgg16”,解决方案非常简单。 See https://github.com/machrisaa/tensorflow-vgg/blob/master/test_vgg16.py 请参阅https://github.com/machrisaa/tensorflow-vgg/blob/master/test_vgg16.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM