2010-06-08 10 views
5

का उपयोग कर मैं अपने वेब कैमरा प्रदर्शित करने के लिए इस अजगर स्क्रिप्ट का उपयोग कर रहा PyQt4 में एक वेब कैमरा धारा में दें:OpenCV कैमरा कैद

from opencv.cv import * 
from opencv.highgui import * 

import sys 

cvNamedWindow("w1", CV_WINDOW_AUTOSIZE) 
camera_index = 0 
capture = cvCreateCameraCapture(camera_index) 

def repeat(): 
    global capture #declare as globals since we are assigning to them now 
    global camera_index 
    frame = cvQueryFrame(capture) 
    cvShowImage("w1", frame) 
    c = cvWaitKey(10) 

    if c == "q": 
     sys.exit(0) 

if __name__ == "__main__": 
    while True: 
     repeat() 

यह काफी अच्छी तरह से काम कर रहा है, लेकिन मैं अपने क्यूटी आवेदन के अंदर इस प्रदर्शन सेट करना चाहते हैं। मैं IplImage ओपनसीवी छवि का उपयोग क्यूटी VideoWidget में कैसे कर सकता हूं?

+0

http://code.google.com/p/opencv-extension-library/wiki/QtOpenCV बहुत अच्छा लगता है, लेकिन कैसे अजगर के साथ यह करने के लिए? – Natim

उत्तर

2

मैं एक QImage करने के लिए एक Iplimage objet गुप्त bellow कोड का इस्तेमाल किया। सही प्रारूप प्राप्त करने में मुझे कुछ समय लगा। Iplimage बीजीआर चैनल ऑर्डर के साथ एक 3-चैनल प्रारूप है जबकि QImage आरजीबी चैनल ऑर्डर का उपयोग करता है।

camcapture = cv.CaptureFromCAM(0)  
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280) 
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720); 

frame = cv.QueryFrame(camcapture) 
image = QImage(frame.tostring(), frame.width, frame.height, QImage.Format_RGB888).rgbSwapped() 
pixmap = QPixmap.fromImage(image) 
3

ऐसा लगता है कि हम अजगर में इस सी ++ कोड बाध्य करने के लिए की जरूरत है:

QImage& cvxCopyIplImage(const IplImage *pIplImage, QImage &qImage) 
{ 
     if(!CV_IS_IMAGE(pIplImage)) return qImage; 

     int w = pIplImage->width; 
     int h = pIplImage->height; 


     if(qImage.width() != w || qImage.height() != h) 
     { 
       qImage = QImage(w, h, QImage::Format_RGB32); 
     } 

     int x, y; 
     for(x = 0; x < pIplImage->width; ++x) 
     { 
       for(y = 0; y < pIplImage->height; ++y) 
       { 
         CvScalar color = cvGet2D(pIplImage, y, x); 

         if(pIplImage->nChannels == 1) 
         { 
           int v = color.val[0]; 

           qImage.setPixel(x, y, qRgb(v,v,v)); 
         } 
         else 
         { 
           int r = color.val[2]; 
           int g = color.val[1]; 
           int b = color.val[0]; 

           qImage.setPixel(x, y, qRgb(r,g,b)); 
         } 
       } 
     } 

     if(pIplImage->origin != IPL_ORIGIN_TL) 
     { 
       qImage = qImage.mirrored(false, true); 
     } 

     return qImage; 
} 
+0

मैंने अभी तक यह नहीं किया है अगर आपने किया: पी – Natim

0

यह एक मेरे लिए काम किया। मैं अजगर 2.6.5 के साथ लिनक्स पर काम करते हैं:

import base64 
import Image 
import time 
import urllib2 
import cv 

# Basic HTTP Authentication... 
url = 'http://192.168.0.11:82/Videostream.cgi' 
ww = 'Username:Password' 
encodedstring = base64.encodestring(ww)[:-1] 
auth = "Basic %s" % encodedstring 
req = urllib2.Request(url,None, {"Authorization": auth }) 
handle = urllib2.urlopen(req) 

def read_stream(): 
    buf = '' 
    b = handle.readlines(45) 
    for a in b: 
     if a.startswith('Content-Length'): 
      readlen = str(a).split()[1] 
    b1 = handle.read(int(readlen)+4) 
    return b1 

def test(): 
    pass 

def write_stream(): 
    imgc = read_stream() 
    cv_img = cv.CreateImageHeader((640,480), cv.IPL_DEPTH_8U, 3) 
    buf = Image.fromstring('RGB',(640,480),imgc[2:], 'jpeg', 'RGB', None) 
    cv.SetData(cv_img, buf.tostring(), (640*3)) 
    return cv_img 


fps = 10.0 

if __name__ == "__main__": 
    while True: 
     frame = write_stream() 
     cv.ShowImage('Camera', frame) 
     k = cv.WaitKey(10) 
     time.sleep(int(1.0/fps)) 
     if k == 0x10001b: # ESC 
      cv.DestroyWindow("Camera") 
      print 'ESC pressed. Exiting ...' 
      break 
+0

असल में यह कोड वेबकैम को क्यूटी विजेट में प्रदर्शित नहीं करता है, है ना? – Natim