Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the artificial intelligence photo album Mini Program based on Serverless

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how the artificial intelligence album Mini Program based on Serverless is realized. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

In our daily life, we often want to "search for photos". Whenever looking for a very old photo, the memory is blurred, when retrieving the photo can only think of the approximate time, and then look at one by one. This is not only inefficient, but also often leaves out the photos we are looking for.

WeChat Mini Programs has developed rapidly in recent years, if there is such a software, we only need to use a simple text description, we can achieve rapid image retrieval, isn't it great!

This project will take Mini Program as an example to develop on the Serverless architecture. The Mini Program adds artificial intelligence search to retain the basic functions of photo albums (create photo albums, delete photo albums, upload pictures, view pictures, delete pictures), that is, the process of automatically describing pictures based on Image Caption technology and realizing Image to Text after users upload pictures. In this way, when the user searches, the closest picture can be returned through the similarity between the text.

Foundation design

The project design mainly has login, photo album, picture upload and preview functions, as well as search functions. As shown in the figure:

The main function of the registration function is to store the user's information in the database by obtaining the user's unique id (OpenId in Wechat). All subsequent operations need to be distinguished by this id.

The album function mainly includes the addition, deletion, query and modification of the album.

Picture functions include uploading, deleting and viewing pictures

The main search function is to view the list of pictures corresponding to the specified tag and the list corresponding to the specified search content.

Of course, these four main functions and modules are closely related to the front end, in addition to the back-end asynchronous operation of the two modules, namely, image compression and image description function.

1. Registration function:

The registration function is the action performed by the user after clicking on the registration account.

This action needs to be noted that it is necessary to determine whether the user has already registered before registering. Log in by default if you have registered, otherwise register and log in. When users do not want to register, they can click on the experience program to preview most of the pages of the program. However, the addition, deletion, modification and query of the relevant database can not be realized. The login function page is shown in the figure:

two。 Album function:

When the user registers and logs in, the album-related management can be carried out on the album management page, including editing, deleting and creating. When you add or modify the album, you need to pay attention to whether the album name already exists; when you delete or modify the album, you need to determine whether the user has the permission to operate the album. The prototype of the album function is shown in the figure:

3. Picture function:

Picture functions mainly include picture list, as well as picture acquisition, upload and deletion. In the process of image acquisition and deletion, it is necessary to determine whether the user has the permission to do this operation, and whether the user has the permission to upload to the specified album when uploading. The prototype diagram related to the picture function is shown in.

In the picture function part, in addition to the functions visible on the user side, there are scheduled tasks. When the user uploads the picture, the system will carry out image compression, image description and keyword extraction asynchronously in the background. The overall process is shown in the figure.

4. Search function:

Search function refers to the process of obtaining target data through keywords or user descriptions. This functional prototype diagram is shown in the figure.

The difficulty of this part lies in the process of searching the target data through the user's description. The basic flow of this process is shown in the figure.

Project development 1. Database establishment

The database part mainly establishes the relationship between the related tables and the tables. First, you need to create the tables necessary for the project:

CREATE DATABASE `album`; CREATE TABLE `album`.`tags` (`tid` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR (255) NOT NULL, `remark` TEXT NULL, PRIMARY KEY (`tid`)) ENGINE = InnoDB;CREATE TABLE `album`.Secrety` (`cid`INT NOT NULL AUTO_INCREMENT, `name`VARCHAR (255) NOT NULL, `sorted`INT NOT NULL DEFAULT'1', `user`INT NOT NULL, `remark` TEXT NULL, `publish`DATE NOT NULL, `area`VARCHAR (255) NULL, PRIMARY KEY (`cid`)) ENGINE = InnoDB CREATE TABLE `album`.`users` (`uid`INT NOT NULL AUTO_INCREMENT, `nickname` TEXT NOT NULL, `wechat`NOT NULL, `remark` TEXT NULL, PRIMARY KEY (`uid`)) ENGINE = InnoDB;CREATE TABLE `album`.`photo` (`pid`INT NOT NULL AUTO_INCREMENT, `name`VARCHAR (255) NOT NULL, `small`VARCHAR (255) NOT NULL, `large`VARCHAR (255) NOT NULL, `nicky` INT NOT NULL, `tags`VARCHAR (255) NULL, `remark` TEXT NULL, `creattime` DATE NOT NULL, `creatarea`VARCHAR (255) NOT NULL, `user`INT NOT NULL, PRIMARY KEY (`pid`) ENGINE = InnoDB CREATE TABLE `album`.`photo _ tags` (`ptid` INT NOT NULL AUTO_INCREMENT, `tag` INT NOT NULL, `photo`INT NOT NULL, `remark` INT NULL, PRIMARY KEY (`ptid`)) ENGINE = InnoDB

After creation, gradually add relationships between tables and some constraints:

ALTER TABLE `photo_ tags` ADD CONSTRAINT `photo_tags_tags_ alter` FOREIGN KEY (`tag`) REFERENCES `tags` (`tid`) ON DELETE CASCADE ON UPDATE RESTRICT;ALTER TABLE `photo_ tags` ADD CONSTRAINT `photo_tags_photo_ alter` FOREIGN KEY (`photo`) REFERENCES `photo` (`pid`) ON DELETE CASCADE ON UPDATE RESTRICT;ALTER TABLE `photo`tags` photo_category_ alter` FOREIGN KEY (`photoy`) REFERENCES `usery` (`cid`) ON DELETE CASCADE ON UPDATE RESTRICT;ALTER TABLE `photo`ADD CONSTRAINT `photo_user_ alter` FOREIGN KEY (`user`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE RESTRICT ALTER TABLE `usery` ADD CONSTRAINT `category_user_ alter` FOREIGN KEY (`user`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE RESTRICT;ALTER TABLE `tags` ADD unique (`name`); 2. Let Code fly.

Before using it, you need to have a Tencent Cloud account and activate SCF, COS, APIGW, CDB and other related product permissions.

Clone the project locally and configure your own key information and database information. The configuration file is in serverless.yaml under the cloudFunction directory:

# the overall configuration information of the functions Conf: component: "serverless-global" inputs: region: ap-shanghai runtime: Python3.6 handler: index.main_handler include_common:. / common mysql_host: gz-c****.com mysql_user: root mysql_password: slots configured Mysql_port: 6 hours. 0 mysql_db: album mini_program_app_id: asdsa****dddd mini_program_app_secret: fd340c4****8744ee tencent_secret_id: AKID1y* * l1q0kK tencent_secret_key: cCoJ****FZj5Oa tencent_appid: 1256773370 cos_bucket: 'album-1256773370' domain: album.0duzahn.com

Since I am currently using Serverless Components, there are no global variables and so on. So the global variable component is added here, and the global variable is set here, which can be referenced directly in the later Components, for example:

# create bucket CosBucket: component:'@ serverless/tencent-website' inputs: code: src:. / cos region: ${Conf.region} bucketName: ${Conf.cos_bucket}

Install essential tools: Serverless Framework, Mini Program Cloud Development IDE. Since the background development language of this project is Python, you also need some Python development tools and package management tools (Python version is no less than 3.6)

Install the corresponding dependencies under some folders:

CloudFunction/album/prdiction needs to install Pillow, opencv, tensorflow, jieba

CloudFunction/album/getPhotoSearch requires gensim, jieba and collections to be installed

CloudFunction/album/compression requires Pillow to be installed

(note that you must use the CentOS operating system when installing. If there is no corresponding system, you can package the corresponding dependencies here: http://serverless.0duzhan.com/app/scf_python_package_download/)

To deploy the project to the cloud, simply use the command serverless-- debug:

DEBUG ─ Resolving the template's static variables. DEBUG ─ Collecting components from the template. DEBUG ─ Downloading any NPM components found in the template. DEBUG ─ Analyzing the template's components dependencies. DEBUG ─ Creating the template's components graph. DEBUG ─ Syncing template state. DEBUG ─ Executing the template's components graph. DEBUG ─ Starting API-Gateway deployment with name APIService in the ap-shanghai region...... DEBUG ─ Updating configure... DEBUG ─ Created function Album_Get_Photo_Search successful DEBUG ─ Setting tags for function Album_Get_Photo_Search DEBUG ─ Creating trigger for function Album_Get_Photo_Search DEBUG ─ Deployed function Album_Get_Photo_Search successful DEBUG ─ Uploaded package successful / Users/dfounderliu/Documents/code/AIAlbum/.serverless/Album_Prediction.zip DEBUG ─ Creating function Album_Prediction DEBUG ─ Updating code... DEBUG ─ Updating configure... DEBUG ─ Created function Album_Prediction successful DEBUG ─ Setting tags for function Album_Prediction DEBUG ─ Creating trigger for function Album_Prediction DEBUG ─ Trigger timer: timer not changed DEBUG ─ Deployed function Album_Prediction successful Conf: region: ap-shanghai...-path: / photo/delete method: ANY apiId: api-g9u6r9wq-path: / Album/delete method: ANY apiId: api-b4c4xrq8-path: / album/add method: ANY apiId: api-ml6q5koy 156s > APIService > done

In this process, all the functions are deployed in 156s. Then open the id of Mini Program and bring it into the miniProgram directory, and fill in your own appid in line 17 of the file project.config.json. At the same time, you need to configure the basic directory of your project, that is, the address returned to us by the API gateway, and write it on line 10 of app.js, so that the project can run.

The above is the editor for you to share the Serverless-based artificial intelligence photo album Mini Program is how to achieve, if you happen to have similar doubts, you may wish to refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

  • Brief introduction of JEESZ distributed Framework

    Introduction 1. Screenshot of the core code structure of the project

    © 2024 shulou.com SLNews company. All rights reserved.

    12
    Report