使用Python和OpenCV将捕获的图像保存到MySQL数据库

使用Python和OpenCV将捕获的图像保存到MySQL数据库,python,mysql,opencv,Python,Mysql,Opencv,我有一段代码,可以从笔记本电脑上的内部网络摄像头捕获图像。有了这个图像,我想把它直接发送到我的MySQL数据库。这是代码 import cv2 import mysql.connector from mysql.connector import errorcode from time import sleep import serial # Obtain connection string information from the portal config = { 'host':'our

我有一段代码,可以从笔记本电脑上的内部网络摄像头捕获图像。有了这个图像,我想把它直接发送到我的MySQL数据库。这是代码

import cv2
import mysql.connector
from mysql.connector import errorcode
from time import sleep
import serial

# Obtain connection string information from the portal
config = {
  'host':'oursystem.mysql.database.azure.com',
  'user':'user',
  'password':'pass',
  'database':'projectdb'
}

try:
   conn = mysql.connector.connect(**config)
   print("Connection established")
except mysql.connector.Error as err:
  if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
    print("Something is wrong with the user name or password")
  elif err.errno == errorcode.ER_BAD_DB_ERROR:
    print("Database does not exist")
  else:
    print(err)

cursor = conn.cursor()
cursor.execute("CREATE TABLE if not exists Camera (img BLOB);")
cap = cv2.VideoCapture(0)

# Check if the webcam is opened correctly
if not cap.isOpened():
    raise IOError("Cannot open webcam")

frame = cap.read()[1]
cursor.execute("INSERT INTO Camera (img) VALUES %s",frame)
cap.release()

正如你们所看到的,我试图直接发送一个图像,该图像被捕获并保存在数据库中的“frame”变量中,但它不起作用

请有人帮帮我

错误

mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'ndarray' cannot be converted to a MySQL type


您可以将图像转换为base64字符串,并将其作为文本feild存储到mysql数据库中。在显示时,您可以对图像进行base64_解码并显示


我如何使用python实现这一点?浏览下面的链接这可能会帮助您@chr Hamza您需要将列blob的类型更改为text。@NIRAMAL TANDEL我将类型更改为LONGBLOB,它就正常工作了。我没有试过使用LONGBLOB类型。谢谢你让我知道。