Drag and Drop Object Detection

Hello. I am looking to implement a program that opens a PyQt Drag and Drop GUI where I can drag a image file and it will show the image with detections on screen. I am doing the detection part exactly like the detect-net.py does it but I keep getting error: detections = net.Detect(img) Exception: jetson.utils – function wasn’t passed a valid cudaImage or cudaMemory object

Below is my code

import sys
import os
from PyQt4 import QtGui, QtCore
import jetson.inference
import jetson.utils



class TestListView(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(72, 72))

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
                net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
                input = jetson.utils.videoSource(str(url.toLocalFile()))
                display = jetson.utils.videoOutput("display://0")
                while display.IsStreaming():
                    img = input.Capture
                    detections = net.Detect(img)
                    
                    display.Render(img)
	                
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            event.ignore()

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        for url in l:
            if os.path.exists(url):
                print(url)                
                icon = QtGui.QIcon(url)
                pixmap = icon.pixmap(72, 72)                
                icon = QtGui.QIcon(pixmap)
                item = QtGui.QListWidgetItem(url, self.view)
                item.setIcon(icon)        
                item.setStatusTip(url)        

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

DropEvent is where the detection actually takes place

Hi,

Is this a typo?

img = input.Capture ()

The Capture should be a function rather than a variable.

Thanks.

Oh yes that is a typo. I fixed that. New error is similar however:

img = input.Capture()
Exception: jetson.utils – videoSource failed to capture image

any tips?

Hi @user128245, can you provide the console log? Perhaps it can’t find that file on disk.

BTW if you are just loading images, it may be simpler to use img = jetson.utils.loadImage(filename)

Also, you may want to move net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5) to the init() function of your class so that it only loads the detection model once, as opposed to every time you drag-n-drop another image.

Im not sure why this fixed it, but I eliminated the while loop and it worked. And yes I probably should move the net to the init(). Thank you!

Ah right - it probably succeeded the first iteration of the while loop, but then failed on subsequent iterations because there were no files left to read. jetson.utils.videoSource() is able to handle directories of images.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.