How to: Build Your Own Business Card Scanner
How to: Build Your Own Business Card Scanner
This is an excellent and very practical project. Building your own business card management system is a great way to handle contacts exactly how you want. Let's break this down into manageable steps.
This is a fantastic full-stack project. We'll build a web application that you can host yourself. It will handle image uploads, process them to find contact details, store them, and allow you to export them.
🚀 Overview of the Solution
Here’s a high-level look at the components we'll need to build:
- Frontend (The User Interface): A simple web page where you can upload an image of a business card and see your list of saved contacts. We'll use HTML, CSS, and JavaScript.
- Backend (The Brains): A server that receives the uploaded image, processes it, and saves the information. We'll use Python with the Flask framework because it's great for building web applications and has powerful tools for our needs.
- OCR (Optical Character Recognition): This is the magic part that reads the text from the image. We'll use an open-source tool called Tesseract, which supports over 100 languages.
- Database (The Memory): A place to store the extracted contact information. We'll start with SQLite, a simple file-based database that's perfect for this kind of project.
-
vCard Generation: A
function to bundle the contact information into a
.vcf
file, which is the standard format for digital business cards.
Regarding the WhatsApp integration, this is the most challenging part due to WhatsApp's strict API rules. Direct integration into the catalog is not possible. However, I'll show you a highly effective and standard workaround.
Let's get started!
🛠️ Step 1: Setting Up Your Backend with Python and Flask
First, we'll create the backend server. This server will have two main jobs:
- An endpoint to receive an image, run OCR, and extract text.
-
An endpoint to generate a
.vcf
file from contact data.
Prerequisites
You'll need to install a few things on your computer:
-
Python: If you don't have it, download it from python.org.
-
Tesseract OCR Engine: This is the core OCR tool. You must install it separately from the Python library.
- Windows: Follow the instructions at Tesseract at UB Mannheim.
-
macOS: Use
Homebrew:
brew install tesseract
-
Linux (Ubuntu/Debian):
sudo apt-get install tesseract-ocr
-
Python Libraries: Open your terminal or command prompt and install these libraries:
pip install Flask Pillow pytesseract vobject
-
Flask
: Our web framework. -
Pillow
: For opening and handling images. -
pytesseract
: A Python wrapper for the Tesseract engine. -
vobject
: To easily create vCard (.vcf
) files.
-
Backend Code (app.py
)
Create a new file named app.py
and paste the following code into it. I've added comments to explain
each part.
How to Run It
-
Save the code above as
app.py
. - Open your terminal in the same directory.
-
Run the command:
python app.py
-
Open your web browser and go to
http://127.0.0.1:5000
.
You should see a simple webpage. Try uploading a business card image! It will
show you the extracted text and a link to download the contact as a .vcf
file.
📲 Step 2: The WhatsApp Integration (The Smart Way)
Directly adding a custom tool to the WhatsApp catalog isn't possible with their current API. The platform is designed for messaging and commerce, not for custom app integrations.
However, here is the professional and most effective solution:
Create a Digital Business Card Page with a QR Code
The web app we just built is the perfect digital business card! You can host it online and then use a QR code to direct people to it.
-
Host Your App: Deploy the
Flask application to a hosting service like Heroku, PythonAnywhere, or
a DigitalOcean Droplet.
This will give you a public URL (e.g.,
https://your-card-scanner.herokuapp.com
). -
Create Your Personal vCard Link: Your app already has the vCard generator. Your personal link would
look like this:
https://<your-app-url>/vcf?name=YourName&email=your.email@server.com&phone=123456789
- Generate a QR Code: Use a free online QR code generator to turn the link from step 2 into a QR code image.
-
Integrate with WhatsApp:
- Profile Picture: You can add the QR code to a corner of your WhatsApp Business profile picture.
-
Quick Replies: Set up a
quick reply in WhatsApp Business. When someone asks for your contact,
you can use a shortcut (like
/contact
) to instantly send a message like: "Here's my digital business card! Just scan the QR code to add me to your contacts." along with the QR code image. - Catalog Link: While you can't add the app itself, you can add a "product" to your catalog named "My Digital Business Card" and use the "Link" field to point to your hosted web app. This is the cleanest integration possible.
This approach is great because anyone with a smartphone can scan the code, and
it directs them to a page where they can instantly download your .vcf
file, which works on both iPhone and Android.
📈 Next Steps and Improvements
This is a great starting point. Here’s how you can make it even better:
-
Improve Text Parsing: The
parse_contact_info
function is very basic. You can significantly improve it using more advanced Regular Expressions (Regex) or even Natural Language Processing (NLP) libraries like spaCy to better identify which text is a name, a title, or a company. - Build a Proper Database: For managing many cards, you should switch from SQLite to a more robust database like PostgreSQL. You would add functionality to save, view, edit, and delete the scanned contacts from your web interface.
- Add User Accounts: If you want others to use your service, you'll need to add user authentication so each person can manage their own set of scanned cards.
- Enhance the Frontend: Use a JavaScript framework like React or Vue.js to create a much more polished and responsive user interface.
Feel free to ask for the code for any of these improvements. Let's start with this foundation and build from there. Happy coding!
The code we've written so far is the backend. It's the engine that does all the heavy lifting.
Great question. Now let's build on that backend to achieve your goal of scanning physical cards, storing them, and adding them to your phone. This involves three key upgrades:
- A Database: To permanently store the contact info.
- Permanent Image Storage: To save the business card image on the server.
- A Contact List: A new page to view all your saved cards.
Workflow: From Physical Card to Phone Contact
Here’s how the complete process will work for you as the user:
- Access the App: You'll open your phone's web browser (like Chrome or Safari) and go to the URL of your hosted application.
- Scan the Card: The webpage will have an "Upload" button. Tapping it will let you either choose a photo from your library or open your phone's camera to take a picture of the physical business card right there.
- Backend Processing: The photo is sent to your backend server. The server runs OCR, saves the contact details and the image file, and stores everything in a database.
- Save to Phone: The server sends back the extracted info. You'll see a "Download vCard (.vcf)" link. Tapping this link on your phone will automatically prompt you to save the contact directly to your phone's address book (both iPhone and Android do this).
- View & Share: You can go to a "My Contacts" page in the app to see all the cards you've scanned, view their images, and share their vCard files with others.
🛠️ Step 1: Modifying the Backend (app.py
)
Let's update our app.py
file to include database support and image storage. We'll use
Python's built-in sqlite3
for our database, so there's no need to install anything new.
This updated code includes:
- A database initialization function.
- Logic to save images with unique names.
-
Saving scanned contact info to the
contacts.db
file. -
New web pages (
/contacts
and/contact/<id>
) to view your saved cards.
Updated app.py
Code:
🗄️ Step 2: Create the Database Schema
The Python code now expects a database table to exist. We need to define what that table looks like.
-
In the same project folder, create a new file named
schema.sql
. -
Paste the following SQL code into it. This defines the
contacts
table with all the fields we need.
schema.sql
File:
-- schema.sql
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT,
phone TEXT,
company TEXT,
title TEXT,
raw_text TEXT NOT NULL,
image_filename TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
▶️ Step 3: Initialize and Run the Application
-
Initialize the Database: Before you run the app for the first time, you need to create the
contacts.db
file and the table inside it. Open your VS Code terminal (with the(venv)
activated) and run this one-time command:python -c "from app import init_db; init_db()"
You should now see a new
contacts.db
file in your project folder. -
Run the App: Now, start the server just like before:
python app.py
Go to http://127.0.0.1:5000
in your browser. The app now has a link to "View All Scanned
Contacts". When you upload and scan a card, it will redirect you to a
details page showing the image and a big button to download the .vcf
file to your phone's contacts.
Source from https://gist.github.com/isaacandy/06c07d811bf1fee269f83df845613e45