吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1616|回复: 6
收起左侧

[Python 原创] AI为图片添加标签HTTP调用

[复制链接]
梦汐 发表于 2022-12-24 20:14
带模型压缩后600MB不到,只适用于动漫图片标签标记。 I2S[TP)G(UAAA$I6B`SA2LX.png
整合包:https://pan.baidu.com/s/1MOQJiVtMdYKfjWUiqmI3JA?pwd=moxi

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
小森丘 + 1 + 1 用心讨论,共获提升!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

txylyz 发表于 2022-12-25 08:25
抢个板凳,感谢楼主分享,收藏了先!!
xsd173 发表于 2022-12-25 08:58
camyan 发表于 2022-12-25 09:07
yhlg 发表于 2022-12-25 09:49
无限开cmd窗口,弹死我了
YukinoshitaKa 发表于 2022-12-25 10:26
感谢分享  试试看
 楼主| 梦汐 发表于 2022-12-26 23:31
yhlg 发表于 2022-12-25 09:49
无限开cmd窗口,弹死我了

编译的时候可能写了个-w,你重新编译看看

[Python] 纯文本查看 复制代码
# coding=utf-8
from deepdanbooru_onnx import DeepDanbooru
from PIL import Image
import json,io,os,sys,time,re
import http.server
import configparser
import threading
import socket

occupancy = 27513
port = 13609
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def check_port(oport):# bianyi -F ImageTagger.py
    try:# bianyi ImageTagger.py
        s.bind(('localhost', oport))
        return True
    except OSError:
        return False

if not check_port(occupancy):
    print(f'重复运行')
    #time.sleep(5)
    sys.exit()

print('DeepDanbooru...Init')
config = configparser.ConfigParser()
if os.path.exists('DeepDanbooru.ini'):
    config.read('DeepDanbooru.ini')
else:
    config['Proxy'] = {'Proxy':'127.0.0.1:1080'}
    with open('DeepDanbooru.ini','w') as configfile:
        config.write(configfile)
    config.read('DeepDanbooru.ini')
Proxy = config.get('Proxy','Proxy')
try:
    root = os.getcwd()
    if not os.path.exists(root+'/model'):
        os.mkdir(root+'/model')
    model_path=root+'/model/deepdanbooru.onnx'
    tags_path=root+'/model/tags.txt'
    if not os.path.exists(model_path):
        model_path = None
    if not os.path.exists(tags_path):
        tags_path = None
    danbooru = DeepDanbooru(Proxy=Proxy,model_path=model_path,tags_path=tags_path)
except Exception as e:
    print('[Proxy]:'+Proxy)
    print("[Exception]:",e)
    time.sleep(100)
    sys.exit()
print('DeepDanbooru...End')
class ImageServer(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        # 读取请求体中的数据
        if self.headers['Content']=="End":#关闭
            t = threading.Thread(target=shutdown_server)
            s.close()
            t.start()
            sys.exit()
        length = int(self.headers['Content-Length'])
        data = self.rfile.read(length)
        if data:
            img = Image.open(io.BytesIO(data))
            res = danbooru(img)
            self.send_response(200)
            self.send_header('Content-Type', 'text/json')
            self.end_headers()
            res=sortJsonData(res)
            otl,tlv = {},{}
            for item in res:
                tlv[item]=transl[item]
            otl['original'] = res
            otl['control'] = tlv
            result = re.match('^"([\s\S]*?)"$', json.dumps(str(otl).replace("'", '"')))
            self.wfile.write(bytes(result.group(1),'UTF-8'))
        else:
            self.send_response(200)
            self.send_header('Content-Type', 'text/plain')
            self.end_headers()
            self.wfile.write(bytes('Me is Online','UTF-8'))
        # 发送响应
# 创建并启动服务器
def shutdown_server():
    server.shutdown()
global transl
transl = None
if os.path.exists('./transl.json'):
    with open('./transl.json', 'r', encoding='utf-8') as f:
        transl = json.load(f)
def sortJsonData(jsondata):
    sorted_jsondata = sorted(jsondata.items(), key=lambda item:item[1], reverse=True) 
    return dict(map(lambda x: (x[0], x[1]), sorted_jsondata))
server = http.server.HTTPServer(('localhost', port), ImageServer)
print('Server:127.0.0.1:'+str(port))
server.serve_forever()
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-1 07:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表