20240309fastapi框架后端嵌入视觉识别算法 上

预训练模型嵌入

模型训练保存代码


import os
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import OneClassSVM
import joblib

# 设置随机种子
np.random.seed(42)
tf.random.set_random_seed(42)

# 加载预训练的VGG16模型(不包括顶层分类器)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# 提取图像特征函数
def extract_features(image):
    image = cv2.resize(image, (224, 224))  # 调整图像大小为224x224
    image = image.astype('float32') / 255.0  # 归一化像素值到[0, 1]
    image = np.expand_dims(image, axis=0)  # 在第0维增加一个维度
    features = base_model.predict(image)  # 提取特征
    features = features.flatten()  # 展平特征
    return features

# 数据路径
normal_video_path = '../Media/Video/no_error_video/2.mp4'  # 正常视频路径
anomaly_video_path = '../Media/Video/error_video/hk/'  # 异常视频路径

# 视频帧存放路径
frames_dir = '../Media/Pic/frames/'  # 存放视频帧的文件夹路径
os.makedirs(frames_dir, exist_ok=True)

# 将正常视频拆分成帧并保存到文件夹
cap = cv2.VideoCapture(normal_video_path)
frame_count = 0
while True:
    ret, frame = cap.read()
    if not ret:
        break
    frame_path = os.path.join(frames_dir, f"normal_frame{frame_count}.jpg")
    cv2.imwrite(frame_path, frame)
    frame_count += 1
cap.release()

# 将异常视频拆分成帧并保存到文件夹
cap = cv2.VideoCapture(anomaly_video_path)
frame_count = 0
while True:
    ret, frame = cap.read()
    if not ret:
        break
    frame_path = os.path.join(frames_dir, f"anomaly_frame{frame_count}.jpg")
    cv2.imwrite(frame_path, frame)
    frame_count += 1
cap.release()

# 提取正常广告帧的特征
normal_features = []
for filename in os.listdir(frames_dir):
    if filename.startswith('normal_frame'):
        image = cv2.imread(os.path.join(frames_dir, filename))
        features = extract_features(image)
        normal_features.append(features)
normal_features = np.array(normal_features)

# 创建异常检测模型
svm_model = OneClassSVM(kernel='rbf', gamma='auto')
svm_model.fit(normal_features)

# 加载待检测的图片
test_image = cv2.imread('../Media/image/error/3.jpg')

# 提取待检测图片的特征
test_features = extract_features(test_image)

# 使用异常检测模型进行预测
test_features = np.expand_dims(test_features, axis=0)
pred = svm_model.predict(test_features)

# 判断预测结果
if pred == -1:
    print("异常广告")
else:
    print("正常广告")




#保存
joblib.dump(svm_model,'../Model/svm_model_guanggao_ver2.pkl')

 模型读取代码

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from sklearn.svm import OneClassSVM
import joblib

# 设置随机种子
np.random.seed(42)
tf.random.set_seed(42)

# 加载预训练的VGG16模型(不包括顶层分类器)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# 提取图像特征函数
def extract_features(image):
    image = cv2.resize(image, (224, 224))  # 调整图像大小为224x224
    image = image.astype('float32') / 255.0  # 归一化像素值到[0, 1]
    image = np.expand_dims(image, axis=0)  # 在第0维增加一个维度
    features = base_model.predict(image)  # 提取特征
    features = features.flatten()  # 展平特征
    return features

# 加载模型
svm_model = joblib.load('路径到你的模型文件/svm_model_guanggao_ver2.pkl')

# 加载待检测的图片
test_image = cv2.imread('路径到你的测试图片/your_test_image.jpg')

# 提取待检测图片的特征
test_features = extract_features(test_image)

# 使用异常检测模型进行预测
test_features = np.expand_dims(test_features, axis=0)
pred = svm_model.predict(test_features)

# 判断预测结果
if pred == -1:
    print("异常广告")
else:
    print("正常广告")

后端代码

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import base64
import pymysql
import pandas as pd

# 测试表
# 读取测试表
def read_test():
	conn = pymysql.connect(host="127.0.0.1",
	                       port=3306,
	                       user="root",
	                       password="123456",
	                       db="aowupt",
	                       charset="utf8")
	cursor = conn.cursor()
	sql = "SELECT * FROM test"
	cursor.execute(sql)
	result = cursor.fetchall()
	return result
# result=load_data_from_mysql()
# #imgdata = base64.b64decode(result[0][2].decode())
# # # #将图片保存为文件
# # # with open("temp.jpg",'wb') as f:
# # #     f.write(imgdata)



# 


app = FastAPI()

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # 允许所有来源,为了安全起见,你应该限制为你的前端应用的实际来源
    allow_credentials=True,
    allow_methods=["*"], # 允许所有方法
    allow_headers=["*"], # 允许所有头
)

# 测试接口
@app.get("/")
async def root():
    image_path=('./image/my_favourite_fll.jpg')
    with open(image_path, "rb") as image_file:
        encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
    return {"message": "博文1","商品2":"我最喜欢的fll","image":encoded_image}

# post图片接口

#get结果接口


可以运行

然后把代码放到get接口里面去

然后我这边写一个前端

<template>
	<view class="content">
	<!-- 	<image class="logo"></image> -->
		<view class="text-area">
			<view class=ans>{{ans}}</view>
			
		</view>
		<view>
			<button class=button_try>判断一下</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imagesrc='/static/logo.png',
				ans:"未判断"
			}
		},
		onLoad() {

		},
		methods: {
			
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.button_try
	{
		margin-top: 10%;
	}
</style>

结果一直连不上。

SyntaxError: Invalid shorthand property initializer at @fs/D:/HBuilderX.3.99.2023122611/HBuilderX/plugins/uniapp-cli-vite/node_modules/@dcloudio/uni-h5-vue/dist/vue.runtime.esm.js:1442

原来是不小心imagesrc=''写了=,改成:即可

imagesrc:'/static/xy.jpg',

ok

<template>
	<view class="content">
		<image :src='imagesrc' class="logo"></image>
		<view class="text-area">
			<view class=ans>{{ans}}</view>	
		</view>
		<view>
			<button class=button_try>判断一下</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imagesrc:'/static/xy.jpg',
				ans:"未判断"
			}
		},
		onLoad() {

		},
		methods: {
			
			
			
			
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
	
	.button_try
	{
		margin-top: 10%;
	}
</style>

问题来了,代码jupyter,py都可以运行,放到后端从浏览器不能获取json报了一个奇怪的错

AttributeError: module 'tensorflow._api.v2.random' has no attribute 'set_random_seed'

没有设置随机种子的方法

 结果是要把import的内容放到函数外面就好了

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import base64
import pymysql
import pandas as pd
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from sklearn.svm import OneClassSVM
import joblib



# 测试表
# 读取测试表
def read_test():
	conn = pymysql.connect(host="127.0.0.1",
	                       port=3306,
	                       user="root",
	                       password="123456",
	                       db="aowupt",
	                       charset="utf8")
	cursor = conn.cursor()
	sql = "SELECT * FROM test"
	cursor.execute(sql)
	result = cursor.fetchall()
	return result
# result=load_data_from_mysql()
# #imgdata = base64.b64decode(result[0][2].decode())
# # # #将图片保存为文件
# # # with open("temp.jpg",'wb') as f:
# # #     f.write(imgdata)



# 


app = FastAPI()

# 添加CORS中间件
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"], # 允许所有来源,为了安全起见,你应该限制为你的前端应用的实际来源
    allow_credentials=True,
    allow_methods=["*"], # 允许所有方法
    allow_headers=["*"], # 允许所有头
)



# 测试接口
@app.get("/")
async def root():
    # image_path=('./image/my_favourite_fll.jpg')
    # with open(image_path, "rb") as image_file:
    #     encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
    return {"message": "博文1","商品2":"我最喜欢的fll"}









# 算法接口
@app.get("/vgg16-itree")
async def root():


    # 设置随机种子
    np.random.seed(42)
    tf.random.set_seed(42)

    # 加载预训练的VGG16模型(不包括顶层分类器)
    base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

    # 提取图像特征函数
    def extract_features(image):
        image = cv2.resize(image, (224, 224))  # 调整图像大小为224x224
        image = image.astype('float32') / 255.0  # 归一化像素值到[0, 1]
        image = np.expand_dims(image, axis=0)  # 在第0维增加一个维度
        features = base_model.predict(image)  # 提取特征
        features = features.flatten()  # 展平特征
        return features

    # 加载模型
    svm_model = joblib.load('../model/svm_model_guanggao_ver2.pkl')

    # 加载待检测的图片
    test_image = cv2.imread('../data/image/uni.png')

    # 提取待检测图片的特征
    test_features = extract_features(test_image)

    # 使用异常检测模型进行预测
    test_features = np.expand_dims(test_features, axis=0)
    pred = svm_model.predict(test_features)

    # 判断预测结果
    if pred == -1:
        ans="异常广告"
        print("异常广告")
    else:
        ans="正常广告"
        print("正常广告")

    return {"answer": ans}


 然后问题又来了

ans成功返回,但是文字变空白不改写,应该是前端代码问题。 

原来我少写了一个data哈哈

 接下来写把图片传后端的post

uni.uploadFile(OBJECT) | uni-app官网 (dcloud.net.cn)

 官网有一个方法

 下一期再试,挖一个坑

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值