Задать вопрос
@IvanTRG

Как запустить openCV на GPU?

есть такой код
import cv2
import numpy as np
import os
import time

# Load the MobileNet SSD model
model = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel')

# Define classes of objects to detect
classNames = {0: 'background', 1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat',
              5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair', 10: 'cow',
              11: 'diningtable', 12: 'dog', 13: 'horse', 14: 'motorbike', 15: 'person',
              16: 'pottedplant', 17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor'}

# открываем видеопоток
cap = cv2.VideoCapture(0)

while True:
    # Считываем кадр с видеопотока
    ret, frame = cap.read()
    img2=cv2.flip(frame, 1)

    frame_re1 = img2[1:200, 1:180] 
    frame_re2 = img2[1:200, 180:360] 
    frame_re3 = img2[1:200, 360:540] 
    frame_re4 = img2[1:200, 540:720] 
    frame_re5 = img2[200:500, 1:180] 
    frame_re6 = img2[200:500, 180:360] 
    frame_re7 = img2[200:500, 360:540] 
    frame_re8 = img2[200:500, 540:720]

    # Resize the frame to a fixed size for processing
    frame_resized1 = cv2.resize(frame_re1, (300, 300))
    frame_resized2 = cv2.resize(frame_re2, (300, 300))
    frame_resized3 = cv2.resize(frame_re3, (300, 300))
    frame_resized4 = cv2.resize(frame_re4, (300, 300))
    frame_resized5 = cv2.resize(frame_re5, (300, 300))
    frame_resized6 = cv2.resize(frame_re6, (300, 300))
    frame_resized7 = cv2.resize(frame_re6, (300, 300))
    frame_resized8 = cv2.resize(frame_re7, (300, 300))

    # Конвертирование файла в большой массив двоичного кода
    blob1 = cv2.dnn.blobFromImage(frame_resized1, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob2 = cv2.dnn.blobFromImage(frame_resized2, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob3 = cv2.dnn.blobFromImage(frame_resized3, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob4 = cv2.dnn.blobFromImage(frame_resized4, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob5 = cv2.dnn.blobFromImage(frame_resized5, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob6 = cv2.dnn.blobFromImage(frame_resized6, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob7 = cv2.dnn.blobFromImage(frame_resized7, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    blob8 = cv2.dnn.blobFromImage(frame_resized8, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)

    # Начинаем распознавание объектов
    model.setInput(blob1)
    detections1 = model.forward()
    model.setInput(blob2)
    detections2 = model.forward()
    model.setInput(blob3)    
    detections3 = model.forward()
    model.setInput(blob4)    
    detections4 = model.forward()
    model.setInput(blob5)    
    detections5 = model.forward()
    model.setInput(blob6)    
    detections6 = model.forward()
    model.setInput(blob7)    
    detections7 = model.forward()
    model.setInput(blob8)    
    detections8 = model.forward()


    
    

    # Рисуем рамки вокруг объектов
   # for i in range(detections.shape[2]):
      #  confidence = detections[0, 0, i, 2]
      #  if confidence > 0.5:
      #      class_id = int(detections[0, 0, i, 1])
       #     class_name = classNames[class_id]
      #      box = detections[0, 0, i, 3:7] * np.array([frame.shape[1], frame.shape[0], frame.shape[1], frame.shape[0]])
      #      x1, y1, x2, y2 = box.astype('int')
      #      cv2.rectangle(img2, (x1, y1), (x2, y2), (0, 0, 255), 2)
       #     cv2.putText(img2, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)


    # Показываем изображение в специальном окне
    #cv2.imshow('Object Detection', img2)
      
    
    
    for i in range(detections1.shape[2]):
        confidence = detections1[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections1[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections1[0, 0, i, 3:7] * np.array([frame_re1.shape[1], frame_re1.shape[0], frame_re1.shape[1], frame_re1.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re1, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re1, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 1',frame_re1)                                                                                              # зона 1
 
    #ret2,frame2 = cap.read()    
    #flip2 = cv2.flip(frame2, 1)
    for i in range(detections2.shape[2]):
        confidence = detections2[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections2[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections2[0, 0, i, 3:7] * np.array([frame_re2.shape[1], frame_re2.shape[0], frame_re2.shape[1], frame_re2.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re2, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re2, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 2',frame_re2) # зона 2

    #ret3,frame3 = cap.read()    
    #flip3 = cv2.flip(frame3, 1)
    for i in range(detections3.shape[2]):
        confidence = detections3[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections3[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections3[0, 0, i, 3:7] * np.array([frame_re3.shape[1], frame_re3.shape[0], frame_re3.shape[1], frame_re3.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re3, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re3, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 3',frame_re3)  # зона 3

    #ret4,frame4 = cap.read()    
    #flip4 = cv2.flip(frame4, 1)
    for i in range(detections4.shape[2]):
        confidence = detections4[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections4[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections4[0, 0, i, 3:7] * np.array([frame_re4.shape[1], frame_re4.shape[0], frame_re4.shape[1], frame_re4.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re4, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re4, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 4',frame_re4)  # зона 4



    #re5,fram5 = cap.read()    
    #flip5 = cv2.flip(frame, 1)
    for i in range(detections5.shape[2]):
        confidence = detections5[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections5[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections5[0, 0, i, 3:7] * np.array([frame_re5.shape[1], frame_re5.shape[0], frame_re5.shape[1], frame_re5.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re5, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re5, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 5',frame_re5)   # зона 5
 
    #ret6,frame6 = cap.read()    
    #flip6 = cv2.flip(frame6, 1)
    for i in range(detections6.shape[2]):
        confidence = detections6[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections6[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections6[0, 0, i, 3:7] * np.array([frame_re6.shape[1], frame_re6.shape[0], frame_re6.shape[1], frame_re6.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re6, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re6, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 6',frame_re6)   # зона 6

    #ret7,frame7 = cap.read()    
    #flip7 = cv2.flip(frame3, 1)


    for i in range(detections7.shape[2]):
        confidence = detections7[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections7[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections7[0, 0, i, 3:7] * np.array([frame_re7.shape[1], frame_re7.shape[0], frame_re7.shape[1], frame_re7.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re7, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re7, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.imshow('zone 7',frame_re7)  # зона 7

    #ret8,frame8 = cap.read()    
    #flip8 = cv2.flip(frame7, 1)
    for i in range(detections8.shape[2]):
        confidence = detections8[0, 0, i, 2]
        if confidence > 0.5:
            class_id = int(detections8[0, 0, i, 1])
            class_name = classNames[class_id]
            box = detections8[0, 0, i, 3:7] * np.array([frame_re8.shape[1], frame_re8.shape[0], frame_re8.shape[1], frame_re8.shape[0]])
            x1, y1, x2, y2 = box.astype('int')
            cv2.rectangle(frame_re8, (x1, y1), (x2, y2), (0, 0, 255), 2)
            cv2.putText(frame_re8, class_name, (x1, y1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 
    cv2.imshow('zone 8',frame_re8)  # зона 8

   # print(class_name)

    # При нажатии клавиши q код прекращает свою работу
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Прекращаем работу вспомогательных систем
cap.release()
cv2.destroyAllWindows()


Есть вот такой код, и я бы хотел запустить его на видюхе, так как на процессоре он вообще не фурычит. Почитал инет и там пишут, что opencv нельзя запускать на видеокарте, но некоторые пишут, что можно, но не знаю как. Может кто подскажет как мне быть?
  • Вопрос задан
  • 399 просмотров
Подписаться 1 Средний 9 комментариев
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы