-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a73539
commit 8372d69
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import cv2 | ||
import cvzone | ||
cap = cv2.VideoCapture(0) | ||
cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') | ||
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye_tree_eyeglasses.xml') | ||
num=1 | ||
while True: | ||
k=cv2.waitKey(100) | ||
if k==ord('s'): | ||
num=num+1 | ||
print(num) | ||
if(num<=29): | ||
overlay = cv2.imread(f'static/Glasses/glass3.png', cv2.IMREAD_UNCHANGED) | ||
# overlay = cv2.imread('static/Glasses/glass6.png'.format(num), cv2.IMREAD_UNCHANGED) | ||
|
||
_, frame = cap.read() | ||
gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | ||
faces = cascade.detectMultiScale(gray_scale) | ||
for (x, y, w, h) in faces: | ||
#cv2.rectangle(frame,(x, y), (x+w, y+h), (0, 255, 0), 2) | ||
overlay_resize = cv2.resize(overlay,(w,int(h*0.8))) | ||
frame = cvzone.overlayPNG(frame, overlay_resize, [x, y]) | ||
cv2.imshow('SnapLens', frame) | ||
if cv2.waitKey(10) == ord('q') or num>29: | ||
break | ||
|
||
|
||
cap.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from flask import Flask, render_template, request, redirect, url_for | ||
import cv2 | ||
import os | ||
|
||
app = Flask(__name__) | ||
app.config['UPLOAD_FOLDER'] = 'static/uploads' | ||
|
||
glasses_overlay = None | ||
|
||
@app.route('/') | ||
def upload(): | ||
return render_template("index.html") | ||
|
||
@app.route('/success', methods=['POST']) | ||
def success(): | ||
global glasses_overlay | ||
|
||
if request.method == 'POST': | ||
f = request.files['file'] | ||
uploaded_path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename) | ||
f.save(uploaded_path) | ||
|
||
# Load the uploaded image using OpenCV | ||
glasses_overlay = cv2.imread(uploaded_path, cv2.IMREAD_UNCHANGED) | ||
|
||
return redirect(url_for('tryon')) | ||
|
||
@app.route('/tryon', methods=['GET']) | ||
def tryon(): | ||
if glasses_overlay is None: | ||
return "Please upload an image first." | ||
|
||
cap = cv2.VideoCapture(0) # Open webcam | ||
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml') | ||
|
||
while True: | ||
ret, frame = cap.read() | ||
if not ret: | ||
break | ||
|
||
gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | ||
faces = face_cascade.detectMultiScale(gray_scale, scaleFactor=1.1, minNeighbors=5) | ||
|
||
for (x, y, w, h) in faces: | ||
overlay_resize = cv2.resize(glasses_overlay, (w, int(h * 0.8))) | ||
|
||
for i in range(overlay_resize.shape[0]): | ||
for j in range(overlay_resize.shape[1]): | ||
if overlay_resize[i, j, 3] != 0: # Check the alpha channel | ||
frame[y + i, x + j, :3] = overlay_resize[i, j, :3] # Overlay glasses image | ||
|
||
cv2.imshow('Virtual Try-On', frame) | ||
|
||
if cv2.waitKey(10) == ord('e'): | ||
break | ||
|
||
cap.release() | ||
cv2.destroyAllWindows() | ||
|
||
return "Virtual try-on completed." | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |