回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页
best-icon

AIGC2024_智绘水墨:用AI生成孩子的专属水墨画 简单

头像 _深蓝_ 2024.11.20 184 0

AIGC2024_智绘水墨:用AI生成孩子的专属水墨画

 

    在数字化时代,人工智能技术正以前所未有的速度渗透到我们生活的方方面面,尤其是在艺术创作领域。本文将介绍一个创新项目——使用讯飞开放平台星火认知大模型的图片生成(HiDream)和图片理解技术,来生成符合幼儿绘画及认知要求的水墨画。

 

一、项目背景

 

    随着人工智能技术的发展,AIGC(AI Generated Content)技术逐渐成为艺术创作的新趋势。我的孩子正在上幼儿园大班,假期参加了画画班,老师布置了一项作业——画一幅水墨画。然而,市面上的水墨画图片大多不适合幼儿的认知水平。为了解决这一问题,我决定利用AIGC技术,结合云天老师的文章《2024AIGC_行空板涂鸦智绘》,制作一个可以生成孩子自己喜欢的水墨画的项目。 

 

二、成果展示 

徒手绘画1:

sc.png

生成水墨画1:

p_38c580d2-a64e-11ef-af48-00163e61fcb0_wm.jpg

徒手绘画2:

sc1.png
生成水墨画2:

storage.hidreamai.jpg

徒手绘画3:

sc2.png
生成水墨画3:

p_2110d730-a6d4-11ef-a8fa-00163e5cba89_wm.jpg

以下是拍摄的生成视频,并使用必剪app编辑视频,视频中有水印却无法去掉,大概是官方插入的推广广告吧。

三、 生成步骤

    注册讯飞开放平台账户,并实名认证,打开讯飞开放平台控制台(https://console.xfyun.cn/app/myapp)购买星火认知大模型图片理解、图片生成(HiDream),

图片.png
图片.png
图片.png

使用讯飞开放平台提供的API文档和python代码调试APPID,APISecret等Websocket服务接口认证信息是否可用。

测试图片理解代码如下:

代码
import _thread as thread
import base64
import datetime
import hashlib
import hmac
import json
from urllib.parse import urlparse
import ssl
from datetime import datetime
from time import mktime
from urllib.parse import urlencode
from wsgiref.handlers import format_date_time
import websocket  # 使用websocket_client


appid = "xxxxx"    #填写控制台中获取的 APPID 信息
api_secret = "xxxxx"   #填写控制台中获取的 APISecret 信息
api_key ="xxxxx"    #填写控制台中获取的 APIKey 信息
imagedata = open("img_1.png",'rb').read()



imageunderstanding_url = "wss://spark-api.cn-huabei-1.xf-yun.com/v2.1/image"#云端环境的服务地址
text =[{"role": "user", "content": str(base64.b64encode(imagedata), 'utf-8'), "content_type":"image"}]



class Ws_Param(object):
    # 初始化
    def __init__(self, APPID, APIKey, APISecret, imageunderstanding_url):
        self.APPID = APPID
        self.APIKey = APIKey
        self.APISecret = APISecret
        self.host = urlparse(imageunderstanding_url).netloc
        self.path = urlparse(imageunderstanding_url).path
        self.ImageUnderstanding_url = imageunderstanding_url

    # 生成url
    def create_url(self):
        # 生成RFC1123格式的时间戳
        now = datetime.now()
        date = format_date_time(mktime(now.timetuple()))

        # 拼接字符串
        signature_origin = "host: " + self.host + "\n"
        signature_origin += "date: " + date + "\n"
        signature_origin += "GET " + self.path + " HTTP/1.1"

        # 进行hmac-sha256进行加密
        signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),
                                 digestmod=hashlib.sha256).digest()

        signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')

        authorization_origin = f'api_key="{self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'

        authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')

        # 将请求的鉴权参数组合为字典
        v = {
            "authorization": authorization,
            "date": date,
            "host": self.host
        }
        # 拼接鉴权参数,生成url
        url = self.ImageUnderstanding_url + '?' + urlencode(v)
        #print(url)
        # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
        return url


# 收到websocket错误的处理
def on_error(ws, error):
    print("### error:", error)


# 收到websocket关闭的处理
def on_close(ws,one,two):
    print(" ")


# 收到websocket连接建立的处理
def on_open(ws):
    thread.start_new_thread(run, (ws,))


def run(ws, *args):
    data = json.dumps(gen_params(appid=ws.appid, question= ws.question ))
    ws.send(data)


# 收到websocket消息的处理
def on_message(ws, message):
    #print(message)
    data = json.loads(message)
    code = data['header']['code']
    if code != 0:
        print(f'请求错误: {code}, {data}')
        ws.close()
    else:
        choices = data["payload"]["choices"]
        status = choices["status"]
        content = choices["text"][0]["content"]
        print(content,end ="")
        global answer
        answer += content
        # print(1)
        if status == 2:
            ws.close()


def gen_params(appid, question):
    """
    通过appid和用户的提问来生成请参数
    """

    data = {
        "header": {
            "app_id": appid
        },
        "parameter": {
            "chat": {
                "domain": "image",
                "temperature": 0.5,
                "top_k": 4,
                "max_tokens": 2028,
                "auditing": "default"
            }
        },
        "payload": {
            "message": {
                "text": question
            }
        }
}

    return data


def main(appid, api_key, api_secret, imageunderstanding_url,question):

    wsParam = Ws_Param(appid, api_key, api_secret, imageunderstanding_url)
    websocket.enableTrace(False)
    wsUrl = wsParam.create_url()
    ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)
    ws.appid = appid
    #ws.imagedata = imagedata
    ws.question = question
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})


def getText(role, content):
    jsoncon = {}
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text


def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length


def checklen(text):
    #print("text-content-tokens:", getlength(text[1:]))
    while (getlength(text[1:])> 8000):
        del text[1]
    return text

if __name__ == '__main__':

    #text.clear
    while(1):
        Input = input("\n" +"问:")
        question = checklen(getText("user",Input))
        answer = ""
        print("答:",end = "")
        main(appid, api_key, api_secret, imageunderstanding_url, question)
        getText("assistant", answer)
        # print(str(text))


测试图片生成代码如下:

 

代码
# -*- encoding:utf-8 -*-
import base64
import hashlib
import hmac
import json
import time
from datetime import datetime
from time import mktime
from urllib.parse import urlencode, urlparse
from wsgiref.handlers import format_date_time
from urllib import parse

import requests

appid = "xxxxxxx"  # 填写控制台中获取的 APPID 信息
apiSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"  # 填写控制台中获取的 APISecret 信息
apiKey = "xxxxxxxxxxxxxxxxxxxx"  # 填写控制台中获取的 APIKey 信息

imagedata = open("data/1.jpg", 'rb').read()
image = str(base64.b64encode(imagedata), 'utf-8')

# 请求地址
create_host_url = "https://cn-huadong-1.xf-yun.com/v1/private/s3fd61810/create"
query_host_url = "https://cn-huadong-1.xf-yun.com/v1/private/s3fd61810/query"


def build_auth_request_url(request_url):
    url_result = parse.urlparse(request_url)
    date = "Thu, 09 May 2024 02:09:13 GMT"  # format_date_time(mktime(datetime.now().timetuple()))
    print(date)
    method = "POST"
    signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(url_result.hostname, date, method, url_result.path)
    signature_sha = hmac.new(apiSecret.encode('utf-8'), signature_origin.encode('utf-8'),
                             digestmod=hashlib.sha256).digest()
    signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
    authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
        apiKey, "hmac-sha256", "host date request-line", signature_sha)
    authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
    values = {
        "host": url_result.hostname,
        "date": date,
        "authorization": authorization
    }
    return request_url + "?" + urlencode(values)


def create_url(url):
    host = urlparse(url).netloc
    path = urlparse(url).path

    # 生成RFC1123格式的时间戳
    now = datetime.now()
    date = format_date_time(mktime(now.timetuple()))

    # 拼接字符串
    signature_origin = "host: " + host + "\n"
    signature_origin += "date: " + date + "\n"
    signature_origin += "POST " + path + " HTTP/1.1"

    # 进行hmac-sha256进行加密
    signature_sha = hmac.new(apiSecret.encode('utf-8'), signature_origin.encode('utf-8'),
                             digestmod=hashlib.sha256).digest()

    signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')

    authorization_origin = f'api_key="{apiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'

    authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')

    # 将请求的鉴权参数组合为字典
    v = {
        "authorization": authorization,
        "date": date,
        "host": host
    }
    # 拼接鉴权参数,生成url
    reUrl = url + '?' + urlencode(v)
    # print(reUrl)
    # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
    return reUrl


def get_headers(url):
    headers = {
        'content-type': "application/json",
        'host': urlparse(url).netloc,
        'app_id': appid
    }
    return headers


def gen_create_request_data(text):
    data = {
        "header": {
            "app_id": appid,
            "status": 3,
            "channel": "default",
            "callback_url": "default",

        },
        "parameter": {
            "oig": {
                "result": {
                    "encoding": "utf8",
                    "compress": "raw",
                    "format": "json"
                },

            }
        },
        "payload": {
            "oig": {
                "text": text
            },
        },
    }
    return data


def create_task():
    text = {
        "image": [image],  #引擎上传的原图,如果仅用图片生成能力,该字段需为空
        "prompt": "请将此图片改为孙悟空大闹天空", #  该prompt 可以是要求引擎生成的描述,也可以结合上传的图片要求模型修改原图
        "aspect_ratio": "1:1",
        "negative_prompt": "",
        "img_count": 4,
        "resolution": "2k"
    }
    b_text = base64.b64encode(json.dumps(text).encode("utf-8")).decode()
    request_url = create_url(create_host_url)
    data = gen_create_request_data(b_text)
    headers = get_headers(create_host_url)
    response = requests.post(request_url, data=json.dumps(data), headers=headers)
    # print(json.dumps(data))
    # return
    print('onMessage:\n' + response.text)
    resp = json.loads(response.text)
    taskid = resp['header']['task_id']
    # print(taskid)
    return taskid




def query_task(taskID):
    data = {
        "header": {
            "app_id": appid,
            "task_id": taskID  # 填写创建任务时返回的task_id
        }
    }
    request_url = create_url(query_host_url)
    headers = get_headers(query_host_url)
    response = requests.post(request_url, data=json.dumps(data), headers=headers)
    res = json.loads(response.content)

    return res


if __name__ == '__main__':
    # 创建任务
    task_id = create_task()

    # 查询结果 task_status 1:待处理 2:处理中 3:处理完成 4:回调完成
    while(True):
       print(datetime.now())
       res =  query_task(task_id)
       code = res["header"]["code"]
       task_status = ''
       if code == 0:
           task_status = res["header"]["task_status"]
           if ('' == task_status):
               print("查询任务状态有误,请检查")
           elif('3' == task_status):
               print(datetime.now())
               print("任务完成")
               print(res)
               f_text = res["payload"]["result"]["text"]
               print("图片信息:\n" + str(base64.b64decode(f_text)))
               break
           else:
               print("查询任务中:......" + json.dumps(res))
               time.sleep(1)
               continue
       else:
           print(res)




结合云天老师《2024AIGC_行空板涂鸦智绘》(https://makelog.dfrobot.com.cn/article-314987.html)的帖子,编写代码,  云天老师的图片理解和图片生成的Websocket服务接口认证信息是相同的,而我的生成的却是不同的,所以写了两个服务接口认证信息。此外修改了生成的图片描述,描述文字为:水墨画,留白,精简细节,电影灯光,水墨设计,4k 高清晰度,超高清,注重细节。

修改代码如下(去掉服务认证接口信息,个人可以申请免费试用):

代码
from unihiker import GUI
import time
import os
import cv2
import numpy as np
import _thread as thread
import base64
import datetime
import hashlib
import hmac
import json
from urllib.parse import urlparse
import ssl
from datetime import datetime
from time import mktime
from urllib.parse import urlencode
from wsgiref.handlers import format_date_time
import websocket  # 使用websocket_client
from urllib import parse
import requests
from PIL import Image
import io
# Hi_dream账户密码
appid = "********"    #填写控制台中获取的 APPID 信息
apiSecret = "********"   #填写控制台中获取的 APISecret 信息
apiKey ="********"    #填写控制台中获取的 APIKey 信息
#图片理解账户
appid1 ="********"    #填写控制台中获取的 APPID 信息
apiSecret1 ="********"   #填写控制台中获取的 APISecret 信息
apiKey1 ="********"    #填写控制台中获取的 APIKey 信息
pos_xy = []
# 请求地址
create_host_url = "https://cn-huadong-1.xf-yun.com/v1/private/s3fd61810/create"
query_host_url = "https://cn-huadong-1.xf-yun.com/v1/private/s3fd61810/query"
class Ws_Param(object):
    # 初始化
    def __init__(self, APPID, APIKey, APISecret, imageunderstanding_url):
        self.APPID = APPID
        self.APIKey = APIKey
        self.APISecret = APISecret
        self.host = urlparse(imageunderstanding_url).netloc
        self.path = urlparse(imageunderstanding_url).path
        self.ImageUnderstanding_url = imageunderstanding_url
    # 生成url
    def create_url(self):
        # 生成RFC1123格式的时间戳
        now = datetime.now()
        date = format_date_time(mktime(now.timetuple()))
        # 拼接字符串
        signature_origin = "host: " + self.host + "\n"
        signature_origin += "date: " + date + "\n"
        signature_origin += "GET " + self.path + " HTTP/1.1"
        # 进行hmac-sha256进行加密
        signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),
                                 digestmod=hashlib.sha256).digest()
        signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')
        authorization_origin = f'api_key="{self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'
        authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
        # 将请求的鉴权参数组合为字典
        v = {
            "authorization": authorization,
            "date": date,
            "host": self.host
        }
        # 拼接鉴权参数,生成url
        url = self.ImageUnderstanding_url + '?' + urlencode(v)
        #print(url)
        # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
        return url
# 收到websocket错误的处理
def on_error(ws, error):
    print("### error:", error)
# 收到websocket关闭的处理
def on_close(ws,one,two):
    print(" ")
# 收到websocket连接建立的处理
def on_open(ws):
    thread.start_new_thread(run, (ws,))
def run(ws, *args):
    data = json.dumps(gen_params(appid=ws.appid, question= ws.question ))
    ws.send(data)
# 收到websocket消息的处理
def on_message(ws, message):
    #print(message)
    data = json.loads(message)
    code = data['header']['code']
    if code != 0:
        print(f'请求错误: {code}, {data}')
        ws.close()
    else:
        choices = data["payload"]["choices"]
        status = choices["status"]
        content = choices["text"][0]["content"]
        print(content,end ="")
        global answer
        answer += content
        # print(1)
        if status == 2:
            ws.close()
def gen_params(appid, question):
    """
    通过appid和用户的提问来生成请参数
    """
    data = {
        "header": {
            "app_id": appid
        },
        "parameter": {
            "chat": {
                "domain": "image",
                "temperature": 0.5,
                "top_k": 4,
                "max_tokens": 2028,
                "auditing": "default"
            }
        },
        "payload": {
            "message": {
                "text": question
            }
        }
}
    return data
def main(appid, apiKey, apiSecret, imageunderstanding_url,question):
    print("appid1:"+appid1)
    wsParam = Ws_Param(appid, apiKey, apiSecret, imageunderstanding_url)
    websocket.enableTrace(False)
    wsUrl = wsParam.create_url()
    ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)
    ws.appid = appid
    #ws.imagedata = imagedata
    ws.question = question
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
def getText(role, content):
    jsoncon = {}
    jsoncon["role"] = role
    jsoncon["content"] = content
    text.append(jsoncon)
    return text
def getlength(text):
    length = 0
    for content in text:
        temp = content["content"]
        leng = len(temp)
        length += leng
    return length
def checklen(text):
    #print("text-content-tokens:", getlength(text[1:]))
    while (getlength(text[1:])> 8000):
        del text[1]
    return text
def build_auth_request_url(request_url):
    url_result = parse.urlparse(request_url)
    date =format_date_time(mktime(datetime.now().timetuple()))
    print(date)
    method = "POST"
    signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(url_result.hostname, date, method, url_result.path)
    signature_sha = hmac.new(apiSecret.encode('utf-8'), signature_origin.encode('utf-8'),
                             digestmod=hashlib.sha256).digest()
    signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
    authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
        apiKey, "hmac-sha256", "host date request-line", signature_sha)
    authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
    values = {
        "host": url_result.hostname,
        "date": date,
        "authorization": authorization
    }
    return request_url + "?" + urlencode(values)
def create_url(url):
    host = urlparse(url).netloc
    path = urlparse(url).path
    # 生成RFC1123格式的时间戳
    now = datetime.now()
    date = format_date_time(mktime(now.timetuple()))
    # 拼接字符串
    signature_origin = "host: " + host + "\n"
    signature_origin += "date: " + date + "\n"
    signature_origin += "POST " + path + " HTTP/1.1"
    # 进行hmac-sha256进行加密
    signature_sha = hmac.new(apiSecret.encode('utf-8'), signature_origin.encode('utf-8'),
                             digestmod=hashlib.sha256).digest()
    signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')
    authorization_origin = f'api_key="{apiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'
    authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
    # 将请求的鉴权参数组合为字典
    v = {
        "authorization": authorization,
        "date": date,
        "host": host
    }
    # 拼接鉴权参数,生成url
    reUrl = url + '?' + urlencode(v)
    # print(reUrl)
    # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
    return reUrl
def get_headers(url):
    headers = {
        'content-type': "application/json",
        'host': urlparse(url).netloc,
        #'app_id': appid
        'app_id': appid
    }
    return headers
def gen_create_request_data(text):
    data = {
        "header": {
            "app_id": appid,
            "status": 3,
            "channel": "default",
            "callback_url": "default",
        },
        "parameter": {
            "oig": {
                "result": {
                    "encoding": "utf8",
                    "compress": "raw",
                    "format": "json"
                },
            }
        },
        "payload": {
            "oig": {
                "text": text
            },
        },
    }
    return data
def create_task(prompt):
    text = {
        "image": [image],  #引擎上传的原图,如果仅用图片生成能力,该字段需为空
        #"prompt": prompt+"请将此图片3D图片", #  该prompt 可以是要求引擎生成的描述,也可以结合上传的图片要求模型修改原图
        "prompt": prompt+"水墨画,留白,精简细节,电影灯光,水墨设计,4k 高清晰度,超高清,注重细节。", #  该prompt 可以是要求引擎生成的描述,也可以结合上传的图片要求模型修改原图
        "aspect_ratio": "1:1",
        "negative_prompt": "",
        "img_count": 1,
        "resolution": "2k"
    }
    b_text = base64.b64encode(json.dumps(text).encode("utf-8")).decode()
    #print("create_url:"+create_url)
    request_url = create_url(create_host_url)
    data = gen_create_request_data(b_text)
    headers = get_headers(create_host_url)
    response = requests.post(request_url, data=json.dumps(data), headers=headers)
    print(json.dumps(data))
    # return
    print('onMessage:\n' + response.text)
    resp = json.loads(response.text)
    taskid = resp['header']['task_id']
    # print(taskid)
    return taskid
def query_task(taskID):
    data = {
        "header": {
            "app_id": appid,
            "task_id": taskID  # 填写创建任务时返回的task_id
        }
    }
    request_url = create_url(query_host_url)
    headers = get_headers(query_host_url)
    response = requests.post(request_url, data=json.dumps(data), headers=headers)
    res = json.loads(response.content)
    return res
# 图像旋转(以原点(0,0)为中心旋转)
def image_rotate(src, rotate=0):
  h,w,c = src.shape
  cos_val = np.cos(np.deg2rad(rotate))
  sin_val = np.sin(np.deg2rad(rotate))
  M = np.float32([[cos_val, -sin_val, 0], [sin_val, cos_val, 0]])
  img = cv2.warpAffine(src, M, (w,h))
  return img
# 事件回调函数,当按钮B被点击时,清除GUI上的内容。
def on_buttonb_click_callback():
    if(bs==2):
        bs=0
        cv2.destroyAllWindows()
    u_gui.clear()
# 当按钮A被点击时,截取屏幕的一部分并保存为图片,然后清除GUI上的内容。
def on_buttona_click_callback():
    global answer,text,bs,image,task_id
    os.system("scrot -a 0,0,240,240 sc.png")
    u_gui.clear()
    imagedata = open("sc.png",'rb').read()
    imageunderstanding_url = "wss://spark-api.cn-huabei-1.xf-yun.com/v2.1/image"#云端环境的服务地址
    text =[{"role": "user", "content": str(base64.b64encode(imagedata), 'utf-8'), "content_type":"image"}]
    Input = "请帮我识别一下画的是什么,你回复的内容如:这幅画描绘的是一棵树。"
    question = checklen(getText("user",Input))
    answer = ""
    print("appid:{}\n".format(appid))
    #print("appid:{}\n".format(appid1))
    main(appid1, apiKey1, apiSecret1, imageunderstanding_url, question)
    getText("assistant", answer)
    image = str(base64.b64encode(imagedata), 'utf-8')
    task_id = create_task(answer)
    bs=1
u_gui=GUI()
u_gui.on_a_click(on_buttona_click_callback)
u_gui.on_b_click(on_buttonb_click_callback)
#当鼠标移动时,记录鼠标的坐标并在GUI上绘制线条。
def mouse_move(x, y):
    global temp_time
    temp_time = time.time()
    pos_xy.append([x,y])
    if len(pos_xy) > 1:
        point_start = pos_xy[0]
        for pos_tmp in pos_xy:
            point_end = pos_tmp
            line_text = u_gui.draw_line(x0=point_start[0],y0=point_start[1],x1=point_end[0],y1=point_end[1],width=5, color=(0,0,0))
            point_start = point_end
# 当鼠标释放时,清除pos_xy列表,停止绘制线条。
def on_release(event):
    pos_xy.clear()
u_gui.master.bind("<ButtonRelease>", on_release)# 抬笔检测
u_gui.on_mouse_move(mouse_move)      #鼠标检测
if __name__ == '__main__':
    # 创建任务
    bs=0
    img = cv2.imread("now.png", cv2.IMREAD_UNCHANGED)
    img1 = cv2.imread("now1.png", cv2.IMREAD_UNCHANGED)
    while True:
    #增加等待,防止程序退出和卡住
        time.sleep(0.5)
        if(bs==1):
           # 创建窗口
           cv2.namedWindow('Resized Image', cv2.WINDOW_NORMAL)
           # 设置窗口属性为全屏
           cv2.setWindowProperty('Resized Image', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
           while(True):
             print(datetime.now())
             res =  query_task(task_id)
             code = res["header"]["code"]
             task_status = ''
             if code == 0:
                 task_status = res["header"]["task_status"]
                 if ('' == task_status):
                     print("查询任务状态有误,请检查")
                 elif('3' == task_status):
                     print(datetime.now())
                     print("任务完成")
                     print(res)
                     f_text = res["payload"]["result"]["text"]
                     # 解码Base64字符串
                     decoded_text = base64.b64decode(f_text).decode('utf-8')
                     # 将JSON字符串转换为Python对象
                     data = json.loads(decoded_text)
                     # 检查data是否是列表,如果是,则遍历列表
                     if isinstance(data, list):
                         for item in data:
                             # 提取image_wm字段
                             image_wm = item.get('image_wm', '')
                             if image_wm:  # 如果image_wm字段存在
                                 print(image_wm)
                                 response = requests.get(image_wm)
                                 # 确保请求成功
                                 if response.status_code == 200:
                                     # 将图片数据转换为numpy数组
                                     image_array = np.asarray(bytearray(response.content), dtype="uint8")
                                     # 使用cv2.imdecode读取图片数据
                                     image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
                                     # 调整图片大小到240x320
                                     image_resized = cv2.resize(image, (240, 320), interpolation=cv2.INTER_AREA)
                                     bs=2
                     break
                 else:
                     print("查询任务中:......" + json.dumps(res))
                     cv2.imshow("Resized Image", img)
                     cv2.waitKey(1000)
                     cv2.imshow("Resized Image", img1)
                     cv2.waitKey(1000)
                     continue
             else:
                 print(res)
        if(bs==2):
            cv2.imshow("Resized Image",image_resized)
            cv2.waitKey(20)

打开mind+,模块加入行空板,打开浏览器为行空板连接网络,新建目录ai绘图,将代码文件复制到ai绘图文件夹下,并将now.png和Now1.png复制到代码文件同一目录,点击运行即可。

图片.png
图片.png
图片.png
图片.png

为行空板联网,成功后看到WIFI状态中出现ip地址。

图片.png
图片.png

运行成功后会出现调试信息中的提示任务完成及图片地址。

图片.png

行空板中出现生成的图片。

图片.png

 

四、出现错误及解决

 

出现错误1:
File "/root/ai绘画.mp/Ai_draw.py", line 32
   cv2.waitKey(1000)
                   ^
IndentationError: unindent does not match any outer indentation level

原因:使用kimi或者百度文心一言发现,这个错误信息表明在你的Python脚本中,cv2.waitKey(1000) 这一行的缩进与之前的代码块不匹配。在Python中,缩进是用来定义代码块的,错误的缩进会导致IndentationError。

解决:

   使用其他代码编辑器如sublime进行预处理,再复制到mind+代码中。

   检查当前行的缩进:看看cv2.waitKey(1000)这一行是使用了空格还是制表符(Tab)进行缩进,以及使用了多少个空格或制表符。

   统一缩进风格:Python社区通常推荐使用4个空格作为缩进单位,并避免混合使用空格和制表符。你可以使用文本编辑器的功能(如Visual Studio Code、PyCharm等)来转换所有的制表符为空格,或者检查并统一缩进风格。

   调整缩进:确保cv2.waitKey(1000)这一行的缩进与它所属的代码块(比如循环、条件语句或函数定义)的缩进一致。

   检查周围的代码:有时候,错误可能不仅仅在出错的那一行,也可能在之前的代码中有未关闭的括号或错误的缩进,导致Python解释器错误地解析后续的代码。

  

出现错误2:

   raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='makelogimg.dfrobot.com.cn', port=443): Max retries exceeded with url: /1725956586787 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa28229b0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution'))

原因:行空板未联网
解决:行空板联网
 

五、后记

 

    通过这次尝试,我深刻体会到了AI技术在教育领域的应用潜力。通过讯飞开放平台的星火认知大模型,我们不仅可以将孩子的涂鸦转换成各种风格的艺术作品,还可以探索更多有趣的应用场景。我相信,在未来的日子里,AI技术将会为我们的教育和生活带来更多的惊喜和便利。

评论

user-avatar