简体   繁体   English

来自URL图像的opencv python面部检测

[英]opencv python face detection from url images

I have no problem getting the opencv face detection using haar feature based cascades working on saved images: 我可以使用基于haar功能的级联对已保存图像进行opencv人脸检测没有问题:

from PIL import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('pic.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

but I can't figure out how to open a url image and pass it into face_cascade. 但我不知道如何打开url图片并将其传递给face_cascade。 I've been playing around with cStringIO, but I don't know what to do with it... 我一直在玩cStringIO,但是我不知道该怎么办...

import cv2.cv as cv
import urllib, cStringIO
img = 'http://scontent-b.cdninstagram.com/hphotos-prn/t51.2885-15/10424498_582114441904402_1105042543_n.png'
file = cStringIO.StringIO(urllib.urlopen(img).read())
source = Image.open(file).convert("RGB")
bitmap = cv.CreateImageHeader(source.size, cv.IPL_DEPTH_8U, 3)
cv.SetData(bitmap, source.tostring())
cv.CvtColor(bitmap, bitmap, cv.CV_RGB2BGR)

is it possible to work with a numpy array instead? 可以使用numpy数组代替吗?

source2 = Image.open(file)
imarr=numpy.array(source2,dtype=numpy.uint8)

I'm a beginner, so I apologize for the poor explanation. 我是一个初学者,因此对解释不佳表示歉意。 thanks a lot in advance!! 提前非常感谢!

Or in another way you can use VideoCapture() class to open url image. 或者,您可以使用VideoCapture()类打开URL图像。

See the C++ code below, 请参阅下面的C ++代码,

VideoCapture cap;

if(!cap.open("http://docs.opencv.org/trunk/_downloads/opencv-logo.png")){
cout<<"Cannot open image"<<endl;
return -1;
}
Mat src;
cap>>src;
imshow("src",src);
waitKey();

In your first example you are using OpenCV2.imread to read your image in the second you are presumably using PIL.Image then trying to convert. 在第一个示例中,您将使用OpenCV2.imread在第二个OpenCV2.imread中读取图像,大概是在使用PIL.Image然后尝试进行转换。

Why not simply save the file to a temp directory and then use OpenCV2.imread again? 为什么不简单地将文件保存到临时目录,然后再次使用OpenCV2.imread

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

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