gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:37:00 25 4
gpt4 key购买 nike

我有一段代码可以从笔记本电脑上的内部网络摄像头捕获图像。有了这个图像,我想将其直接发送到我的 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 字符串并将其作为文本字段存储到您的 mysql 数据库中。在显示时,您可以对图像进行 base64 解码并显示。

https://www.php.net/manual/en/function.base64-decode.php

关于python - 使用 Python 和 OpenCV 将捕获的图像保存到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58140682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com