Пример #1
0
 def __init__(self, filename):
     self.capture = cv.CaptureFromFile(filename)
Пример #2
0
def main():
    """
    Main program - controls grabbing images from video stream and loops around each frame.
    """
    #camera = cv.CaptureFromFile("rtsp://192.168.1.18/live_mpeg4.sdp")
    camera = cv.CaptureFromFile("testcards/sample1.mp4")
    #camera = cv.CaptureFromCAM(0)
    if (camera!=None):
        frameSize = (640,480)
        videoFormat = cv.FOURCC('p','i','m','1')
        vw = cv.CreateVideoWriter("seizure_test.mpg",videoFormat, outputfps,frameSize,1)

        cv.NamedWindow(window1,cv.CV_WINDOW_AUTOSIZE)
        origImg = cv.QueryFrame(camera)
        lastTime = datetime.datetime.now()
        while (origImg):
            # Preprocess, then add the new image to the list, along with the 
            # time it was recorded.
            imgList.append(
                (lastTime,
                 preProcessImage(origImg)
                 ))
            # Drop the oldest image off the list if we have enough in the list.
            if (len(imgList)>IMG_STACK_LEN):
                imgList.pop(0)  # Remove first item
                
 
            xorig = 0
            yorig = 0
            if (len(imgList) == IMG_STACK_LEN):
                # imgList[] is now a list of tuples (time,image) containing the
                # reduced size images -
                spectra = getSpectra(imgList)
                binWidth = 1.0*inputfps/IMG_STACK_LEN
                #(a,fftMax,b,(freqNo,pixelNo))= cv.MinMaxLoc(spectra)
                for freqNo in range(0,int(len(imgList)/2)):
                    for pixelNo in range(0,70):
                        if (abs(spectra[pixelNo,freqNo])>FREQ_THRESH):
                            print "PixelNo %d exceeds threshold (val=%f) in freq bin %d (%f Hz" % (pixelNo,abs(spectra[pixelNo,freqNo]),freqNo,freqNo*binWidth)
                            (xmax,ymax) = pixelNo2xy(pixelNo,imgList[0][1])
                            (xorig,yorig) = getEquivLoc(xmax,ymax,ANALYSIS_LAYER)
                            if (freqNo<10):
                                colour = cv.Scalar(255,1,1)
                                thickness = 1
                            elif (freqNo>10 and freqNo<20):
                                colour = cv.Scalar(1,255,1)
                                thickness = 5
                            elif (freqNo>20 and freqNo<30):
                                colour = cv.Scalar(1,1,255)
                                thickness = 10
                            elif (freqNo>30):
                                colour = cv.Scalar(255,255,255)
                                thickness = 20
                            cv.Circle(origImg, (xorig,yorig), 30, colour, thickness=thickness, lineType=-1, shift=0) 
            cv.WriteFrame(vw,origImg)
            cv.ShowImage(window1,origImg)
            cv.ShowImage(window2,imgList[0][1])
            cv.WaitKey(1) # This is very important or ShowImage doesn't work!!
                

            timeDiff = (datetime.datetime.now() - lastTime).total_seconds() 
            if (timeDiff<1./inputfps):
                print "timediff=%f, 1/fps=%f" % (timeDiff,1./inputfps)
                cv.WaitKey(1+int(1000.*(1./inputfps - timeDiff)))

            # Note - there is something odd about this time calculation
            # it does not seem to be consistent with the timestamps on the
            # images.
            timeDiff = (datetime.datetime.now() - lastTime).total_seconds() 
            fps = 1./timeDiff
            print "timeDiff=%f, fps=%f fps" % (timeDiff,fps)

            # Now get a new frame ready to start the loop again
            origImg = cv.QueryFrame(camera)
            lastTime = datetime.datetime.now()
        print "no more images..."
    else:
        print "Error - failed to connect to camera"
Пример #3
0
def main():
    import argparse
    import logging
    import os
    import yaml
    import cv

    global processes
    global forest0, svmmodels, training_bosts, hist0

    parser = argparse.ArgumentParser()
    parser.add_argument('classifier')
    parser.add_argument('cores',
                        type=int,
                        help='Number of processes of paralellism')
    parser.add_argument(
        '--postprocess',
        action="store_true",
        help='Run postprocessing, close blobs and remove noise')
    args = parser.parse_args()

    logging.basicConfig(level=logging.WARNING,
                        format="%(asctime)s - %(message)s")

    classifier = zipfile.ZipFile(args.classifier)
    forest0, hist0, forest1, hist1, training_bosts, svmmodels, prior = \
        load_from_classifier(classifier)
    classifier.close()

    processes = args.cores
    pool = Pool(processes=processes)

    KEY_FRAME_PERIOD = 2  # in seconds
    q = Manager().Queue()
    total_frame = 0

    new_flag = True
    while True:
        if not new_flag:
            print "wait..."
            time.sleep(1)
        stream_list = get_list(CLOUDLET_RESOURCE, STREAM_RESOURCE)
        #print stream_list
        new_flag = False
        prev_stream = None
        for stream in stream_list:
            if stream.get("stream_description").find(
                    "denatured"
            ) == -1 or stream.get("stream_description").find(
                    "video"
            ) == -1 or stream.get("stream_description").find("ppstf") != -1:
                prev_stream = stream
                continue
            ILP_max = []
            for i in xrange(len(CLASSES)):
                ILP_max.append(0)
            ILP_list = []
            for i in xrange(len(CLASSES)):
                ILP_list.append([])
            path, name = stream.get("path").replace("mnt",
                                                    "cloudletstore").rsplit(
                                                        '/', 1)
            print os.path.join(path, name)
            path_p, name_p = prev_stream.get("path").replace(
                "mnt", "cloudletstore").rsplit('/', 1)
            print os.path.join(path_p, name_p)
            statinfo = os.stat(os.path.join(path_p, name_p))
            prev_stream = stream

            if statinfo.st_size == 0:
                continue

            new_flag = True
            frame_rate = 30

            capture = cv.CaptureFromFile(os.path.join(path, name))
            frame_rate = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
            total_frames = cv.GetCaptureProperty(capture,
                                                 cv.CV_CAP_PROP_FRAME_COUNT)
            frame = cv.QueryFrame(capture)
            print frame_rate, total_frames
            print capture

            start_time = time.time()

            key_frame_counter_base = 0
            while frame:
                process_num = 0
                while frame:
                    cv.SaveImage("indexing" + "%d.png" % process_num, frame)
                    for i in xrange(int(KEY_FRAME_PERIOD * frame_rate)):
                        frame = cv.QueryFrame(capture)
                    process_num += 1
                    if process_num == processes:
                        break
                pool.map(
                    calculate_class,
                    [(q, x)
                     for x in xrange(key_frame_counter_base,
                                     key_frame_counter_base + process_num)])

            while not q.empty():
                q_entry = q.get()
                key_frame_counter = q_entry[0]
                ILP = q_entry[1]
                for class_index, score in enumerate(ILP):
                    if score > SCORE_THRESHOLD:
                        ILP_list[class_index].append(
                            (key_frame_counter *
                             int(KEY_FRAME_PERIOD * frame_rate) + 1, score))
                        print(CLASSES[class_index], "%.02f" % score),
                    if score > ILP_max[class_index]:
                        ILP_max[class_index] = score
                print

                key_frame_counter_base += process_num

            for class_index, frame_list in enumerate(ILP_list):
                if not frame_list:
                    continue
                frame_list_split = split_frame_list(
                    frame_list,
                    int(KEY_FRAME_PERIOD * frame_rate) * 2)
                for frame_list, local_max_score in frame_list_split:
                    tag_entry = {}
                    tag_entry["tag"] = CLASSES[class_index] + ":%d" % (
                        ILP_max[class_index] * 100)
                    tag_entry["tag_value"] = local_max_score
                    tag_entry["offset"] = frame_list[0] / frame_rate
                    tag_entry["duration"] = (frame_list[-1] -
                                             frame_list[0]) / frame_rate
                    tag_entry["segment"] = stream.get("segment")
                    print tag_entry
                    ret_dict = post(CLOUDLET_RESOURCE, TAG_RESOURCE, tag_entry)

            if stream.get("stream_description").find("ppstf") == -1:
                stream_entry = {
                    "stream_description":
                    stream.get("stream_description") + "ppstf;"
                }
                ret_dict = put(CLOUDLET_RESOURCE, stream.get("resource_uri"),
                               stream_entry)

            elapse_time = time.time() - start_time
            print "max score:"
            print[(CLASSES[class_index], "%.02f" % score)
                  for class_index, score in enumerate(ILP_max)]
            print "total time: %.2f, key frames: %d, frame per sec: %.2f" \
               % (elapse_time, key_frame_counter_base, key_frame_counter_base / elapse_time)
            print
Пример #4
0
def main():
    import argparse
    import logging
    import os
    import yaml
    import cv

    parser = argparse.ArgumentParser()
    parser.add_argument('classifier')
    parser.add_argument(
        '--postprocess',
        action="store_true",
        help='Run postprocessing, close blobs and remove noise')
    parser.add_argument('dataset', help='Algum formatted trainingset or image')
    parser.add_argument('cores',
                        type=int,
                        help='Number of processes of paralellism')
    args = parser.parse_args()

    logging.basicConfig(level=logging.WARNING,
                        format="%(asctime)s - %(message)s")

    global name
    name = os.path.splitext(args.dataset)[0]

    classifier = zipfile.ZipFile(args.classifier)
    global forest0, svmmodels, training_bosts, hist0
    forest0, hist0, forest1, hist1, training_bosts, svmmodels, prior = \
        load_from_classifier(classifier)
    classifier.close()

    capture = cv.CaptureFromFile(args.dataset)
    frame_rate = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
    total_frames = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)
    print frame_rate, total_frames

    global processes
    processes = args.cores
    print processes
    pool = Pool(processes=1)

    KEY_FRAME_PERIOD = 2  # in seconds

    start_time = time.time()
    key_frame_counter_base = 0
    frame = cv.QueryFrame(capture)
    while frame:
        process_num = 0
        while frame:
            cv.SaveImage(name + "%d.png" % process_num, frame)
            for i in xrange(int(KEY_FRAME_PERIOD * frame_rate)):
                frame = cv.QueryFrame(capture)
            process_num += 1
            if process_num == processes:
                break

        pool.map(
            calculate_class,
            range(key_frame_counter_base,
                  key_frame_counter_base + process_num))

        key_frame_counter_base += process_num

    elapse_time = time.time() - start_time
    print "total time: %.2f, key frames: %d, frame per sec: %.2f" \
           % (elapse_time, key_frame_counter_base, key_frame_counter_base / elapse_time)
Пример #5
0
#!/usr/bin/python2.7

import sys
import cv
import numpy as np
import Image

#files = sys.argv[1:]

#for f in files:
capture = cv.CaptureFromFile(sys.argv[1])
print capture

print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)

for i in xrange(100):
    frame = cv.QueryFrame(capture)

if frame:
    im = Image.fromstring("RGB", (frame.width, frame.height), frame.tostring())
    pixels = np.array(im.getdata())
    im.save("frame.png")
    print pixels
Пример #6
0
import cv
from numpy import array
from scipy.cluster.vq import vq
from os import system
from Vector_Quantization import code_book

#Configuracoes :
divisoes_para_vetor_de_caracteristicas = 5
filtro_de_gauss = 3
filtro_de_dilatacao = 4
filtro_de_erosao = 2
resolucao_largura = 640
resolucao_altura = 480

video = cv.CaptureFromFile('andar_direita.avi')
frames_total = int( cv.GetCaptureProperty( video, cv.CV_CAP_PROP_FRAME_COUNT ) )

cv.NamedWindow("Video", 1)
#cv.NamedWindow("Mascara", 1)
cv.NamedWindow("Binario", 1)
#cv.NamedWindow("Regiao de Interesse", 1)
#cv.MoveWindow("Regiao de Interesse",650,20)
#cv.MoveWindow("Mascara",650,510)
cv.MoveWindow("Binario",1000,510)

mascara = cv.CreateImage((resolucao_largura,resolucao_altura), 8, 3)
cinza = cv.CreateImage((resolucao_largura,resolucao_altura), 8, 1)
fundo = cv.LoadImage('fundo.jpg')
cv.Smooth(fundo,fundo,cv.CV_GAUSSIAN,filtro_de_gauss)

class Filtros:
Пример #7
0
print 'Contagem de veiculos'

videoEntrada = sys.argv[1]
dirSaida = '../Saida/'
criaDir(dirSaida)
dirSaida = dirSaida + os.path.basename(videoEntrada).replace('.', '_')
#apaga tudo que existir no diretorio de saida do video.
shutil.rmtree(dirSaida, True)
# cria novamente o dir de saida
criaDir(dirSaida)
#cria um diretorio para imagens
criaDir('%s/imagens/' % dirSaida)

# abre o video de entrada
capture = cv.CaptureFromFile(videoEntrada)

width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
fourcc = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)
totalFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))

print 'Informacoes do video de entrada: %s' % videoEntrada
print 'Dimensoes do video de entrada: %dx%d' % (width, height)
print 'Taxa de quadros: %d fps' % (fps)
print 'Total de frames do video: %d' % (totalFrames)

print '?: %d' % (fourcc)

# informacoes para escrever texto sobre o video
Пример #8
0
import cv
import sys

samples_dir = '../../samples'

file = sys.argv[1]
frameskip = 1 + int(sys.argv[2])
capture = cv.CaptureFromFile(file)
n_frames = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)

print("File: %s, frames: %d" % (file, n_frames))

for k in range(n_frames):
    try:
        img = cv.QueryFrame(capture)
        if not (k % frameskip):
            cv.SaveImage(samples_dir + "frame" + str(k) + ".jpg", img)
    except (cv.error):
        print "End of file reached."
Пример #9
0
#!/usr/bin/python
import sys
import cv

video = "http://192.168.1.4:8080/browser.html"
capture = cv.CaptureFromFile(video)
cv.NamedWindow('Video Stream', 1)
while True:
    # capture the current frame
    frame = cv.QueryFrame(capture)
    if frame is None:
        break
    else:
        #detect(frame)
        cv.ShowImage('Video Stream', frame)
    if k == 0x1b:  # ESC
        print 'ESC pressed. Exiting ...'
        break
Пример #10
0
import cv

video = cv.CaptureFromFile('levantar_bracos.avi')
#cv.SetCaptureProperty(video,cv.CV_CAP_PROP_FPS,15)
frames_total = int(cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FRAME_COUNT))
fps = cv.GetCaptureProperty(video, cv.CV_CAP_PROP_FPS)
waitPerFrameInMillisec = int(1 / fps * 1000 / 1)

cv.NamedWindow("Video", 1)

for f in xrange(frames_total):
    #while True :
    print cv.GetCaptureProperty(video, cv.CV_CAP_PROP_POS_MSEC)
    frame = cv.QueryFrame(video)
    cv.ShowImage("Video", frame)
    cv.WaitKey(waitPerFrameInMillisec)
    #if cv.WaitKey(7) % 0x100 == 27:
    #break

print 'Numero de Frames = ', frames_total
print 'Frame Rate = ', fps, ' frames por segundo'
Пример #11
0





# cria o escritores para os videos de saida
writer_saida = cv.CreateVideoWriter('%s/%s' % (dirVideos,ARQ_SAIDA),int(fourcc),fps,(int(nw),int(nh)),1)


# usa o primeiro vídeo como tamanho de todos


captures = []
for i in range(len(arquivos)):
    capture = cv.CaptureFromFile(dirVideos + arquivos[i])
    captures.append(capture)
    
totalFrames = int(cv.GetCaptureProperty(captures[0], cv.CV_CAP_PROP_FRAME_COUNT)) 

print 'Total de frames do video 0: %d' % totalFrames



# verifica cada frame do video
for iframe in xrange(totalFrames):
    
    print 'Processando frame %d de %d' % (iframe,totalFrames)
    
        
    # cria a imagem de saida
Пример #12
0
    def __init__(self, wav, labels):

        path = re.sub('\_audio.wav$', '', wav)
        print(path)

        sample = VideoMat(path, False)
        sk = Skelet(sample)
        #labels = sample.labels

        Head_X, Head_Y = self.get_specific_data(sk.PP, 'Head', sk.joints)
        ShouldC_X, ShouldC_Y = self.get_specific_data(sk.PP, 'ShoulderCenter',
                                                      sk.joints)
        HandL_X, HandL_Y = self.get_specific_data(sk.PP, 'HandLeft', sk.joints)
        HandR_X, HandR_Y = self.get_specific_data(sk.PP, 'HandRight',
                                                  sk.joints)

        path = path + '_color.mp4'  #user or color
        capture = cv.CaptureFromFile(path)

        nFrames = int(
            cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))
        width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
        height = int(
            cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))

        w, h = 160, 160
        #An image, which will contain grayscale version of our frame
        frame_gray = cv.CreateImage((w, h), 8, 1)
        mean_array_upper = np.zeros(nFrames)
        mean_array_lower = np.zeros(nFrames)
        mean_array_neck = np.zeros(nFrames)
        mean_array_left = np.zeros(nFrames)
        mean_array_right = np.zeros(nFrames)

        for f in range(nFrames):
            img = cv.QueryFrame(capture)

            cv.Threshold(img, img, 255, 255, cv.CV_THRESH_BINARY)

            image, face = self.DetectFace(img, int(HandL_X[f]),
                                          int(HandL_Y[f]), 15, 15, False)
            image, face = self.DetectFace(img, int(HandR_X[f]),
                                          int(HandR_Y[f]), 15, 15, False)
            image, face = self.DetectFace(img, int(Head_X[f]), int(Head_Y[f]),
                                          80, 80)

            pos = (face[0], face[1])
            size = (face[2], face[3])

            if pos[0] < 0:
                pos = (0, 0)
            if size[0] > image.width:
                size = (image.width, image.height)

            cropped = cv2.cv.CreateImage(size, 8, 3)
            cv2.cv.Copy(cv2.cv.GetSubRect(image, pos + size), cropped)
            image2 = cv2.cv.CreateImage((w, h), 8, 3)
            cv2.cv.Resize(cropped, image2)

            cv2.cv.CvtColor(image2, frame_gray, cv.CV_RGB2GRAY)

            mean_array_upper[f] = np.mean(np.asarray(frame_gray[:70, :]))
            mean_array_lower[f] = np.mean(np.asarray(frame_gray[70:140, :]))
            mean_array_neck[f] = np.mean(np.asarray(frame_gray[140:160, :]))
            mean_array_left[f] = np.mean(np.asarray(frame_gray[:, :80]))
            mean_array_right[f] = np.mean(np.asarray(frame_gray[:, 80:]))

        #print sample.labels
        array_to_analyze = [
            mean_array_upper, mean_array_lower, mean_array_neck,
            mean_array_left, mean_array_right
        ]
        min_value = np.apply_along_axis(np.min, 1, array_to_analyze)

        number_of_column = len(array_to_analyze)
        number_of_line = len([value for value in labels if value != 0])
        data_frame = [[0] * number_of_column for x in range(number_of_line)]

        for index_interest, interest_array in enumerate(array_to_analyze):
            for index_label, value in enumerate(labels):
                if value != 0:
                    name, tup = value
                    a = ((interest_array[tup[0] - 1:(tup[1] - 1)] >
                          min_value[index_interest])).sum()
                    data_frame[index_label][index_interest] = str(a)
        self.data_frame = data_frame