Is it possible to call a function to execute a MySQL command while using OpenCVs VideoStream in python?

0

Issue

I have a web app (using flask and python) that can read a QR code from a live video stream (using opencv). Is it possible to save the data that has been read to my MySQL database without stopping the stream? Every time I try to call the function to save the data, the stream hangs, giving me a NoneType error. I tried calling the same function outside the stream and it works.

In my model.py, I have this function. :

def saveAttendance(qr_code):
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT user_id, first_name, last_name FROM users WHERE qr_code = %s', [qr_code])
    user_record = cursor.fetchone()
    user_id = user_record['user_id']
    name = user_record['first_name'] + " " + user_record['last_name']
    cursor.execute('INSERT INTO attendance VALUES (NULL, %s, NOW(), NOW())', [user_id])
    mysql.connection.commit()
    cursor.close()
    return name

in my views.py, I have this:

@views.route('/MaskandQRCodeDetection')
def mask():
    return render_template('MaskandQRCodeDetection.html', username=session['username'])
   
def gen(camera):
    while True:
        data = camera.get_frame()
        frame = data[0]
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')


@views.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()), mimetype='multipart/x-mixed-replace; boundary=frame')

And in my camera.py, I have this:

class VideoCamera(object):

    # initialize the video stream
def __init__(self):
    self.stream = VideoStream(src=0).start()

def __del__(self):
    self.stream.stop()

def get_frame(self):
 .
 . 
 . 
 .
 #And this part reads the qr code:

for barcode in decode(frame):
    global prev, myOutput, myData
    myData = barcode.data.decode('utf-8')
     
    #I saved all the QR code in my database to qr_list
    if myData in qr_list:

        # check if mask is detected and if the qr_code is not a duplicate
        if label == 'Mask' and prev != myData:
            # set myOutput with the name of the detected user
            #This is where I have the problem
            myOutput = saveAttendance(myData)

        myColor = (0, 255, 0)
        # update previous qr_code with the current qr_code
        prev = myData

    else:
        myOutput = 'Not recognized'
        myColor = (0, 0, 255)

It gives me the following error when I call the function inside the videostream

Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38\Lib\site-packages\werkzeug\wsgi.py", line 506, in __next__
    return self._next()
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python38\Lib\site-packages\werkzeug\wrappers\base_response.py", line 45, in _iter_encoded
    for item in iterable:
  File "C:\AI\website\views.py", line 78, in gen
    data = camera.get_frame()
  File "C:\AI\website\camera.py", line 157, in get_frame
    myOutput = saveAttendance(myData)
  File "C:\AI\website\model.py", line 117, in saveAttendance
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
AttributeError: 'NoneType' object has no attribute 'cursor'

Thank you so much. Any response will be greatly appreciated.

Solution

Solved this by creating an app context inside the model.py function. It turned out that for some reason mysql.connection is returning none.

def saveAttendance(qr_code):
    with app.app_context():
      .
      .
      .

Answered By – newcomer2020

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More