2017-05-28 54 views
1
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 

我在互联网上找到了这个matlab代码。它显示一个窗口,上面有来自网络摄像头的图片,并且很快也会命名图片中的内容(“键盘”,“bootle”,“铅笔”,“时钟”...)。我想在python中做到这一点。 到目前为止,我有这样的:Python:分类图像中的对象

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 

这是alreay很相似,但只检测面。 matlab代码使用alexnet。我想这是一个基于imagenet数据的预训练网络(http://www.image-net.org/)。但它不再可用。 我将如何在python中做到这一点?

(这里有类似的问题,但它是4岁,我认为现在有更新的技术)。

+0

第一个示例使用1000个类中的一个对象选择标记整个图片。第二个发现图片中只有一个类对象,但包含每个实例的位置。你能否澄清一下,你只想复制Python中的第一个例子?将两者结合也是可能的(围绕多个类别标识对象的框),但是可以比任何一个例子都要复杂得多。 –

+0

我只对“label = classify(nnet,picture)”感兴趣;%分类图片“ I.e.为pciture获得一个分类标签。 – jms

回答