Artificial Intelligence 11 min read

Building a Celebrity Face Recognition System with Baidu API and Python

This article details a step‑by‑step tutorial for creating a celebrity face‑matching application that crawls star information, stores images and metadata in a MySQL database, and uses Baidu's facial recognition API to compare uploaded photos, outputting similarity scores and matched celebrity details.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Building a Celebrity Face Recognition System with Baidu API and Python

The author describes a personal project that builds a face‑matching system for celebrities by first crawling star information from Baidu Baike, downloading their photos, and storing the filenames and details in a MySQL database.

Using Python, the script formats the data, connects to the database, and leverages Baidu's facial recognition API to compare an uploaded image with the stored celebrity images, returning similarity scores and matched information.

Below is the core implementation that handles database connection, image encoding, API request, and result processing:

# encoding:utf-8 import base64, urllib, urllib2, simplejson as json, sys, MySQLdb reload(sys) sys.setdefaultencoding('utf-8') def conmysql(): conn = MySQLdb.connect(host='localhost', port=3306, user='root', passwd='123456', db='xxnlove', charset='utf8') return conn def facecompar(image01, image02): matchUrl = "https://aip.baidubce.com/rest/2.0/face/v2/match" f = open(image01, 'rb') img1 = base64.b64encode(f.read()) f = open(image2, 'rb') img2 = base64.b64encode(f.read()) params = {"images": img1 + ',' + img2} params = urllib.urlencode(params) access_token = '24.1a060b87a0dfcab77317999d.25922220.1505832798.282335-10029360' matchUrl = matchUrl + "?access_token=" + access_token request = urllib2.Request(url=matchUrl, data=params) request.add_header('Content-Type', 'application/x-www-form-urlencoded') response = urllib2.urlopen(request) content = response.read() if content: content = json.loads(content) similar = content['result'][0]['score'] return similar

An optimized version adds web crawling for star information, uses the requests and BeautifulSoup libraries to fetch biographies and images, and stores them in the database, improving automation and scalability.

Running the script compares a test image against the database, reports the number of comparisons performed, and prints the matched celebrity name, birthdate, and similarity score (e.g., "曾轶可 27 1990年1月3日出生于湖南省常德市汉寿县,创作型女歌手,演员。相似度:63.689125061"). The author notes limitations such as inconsistent photo sizes and reliance on Baidu's API, and mentions ongoing learning in data structures and algorithms.

PythonAIMySQLFace Recognitionweb crawlingimage matchingBaidu API
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.