Skip to content

Commit

Permalink
camera integration and updated requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
jp6024 committed Apr 25, 2024
1 parent 0637c39 commit 3e01f4a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
26 changes: 26 additions & 0 deletions web-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import tempfile
import threading
import time
import cv2

# import traceback
from datetime import datetime
Expand Down Expand Up @@ -83,6 +84,31 @@ def processing(image_id):
except bson.errors.InvalidId:
app.logger.error("Invalid image_id provided.")
return jsonify({"error": "Invalid image ID"}), 400

# capturing the photo
@app.route('/capture_photo', methods=['GET'])
def capture_photo():
camera = cv2.VideoCapture(0) # initialize camera

try:
# capture camera frame
success, frame = camera.read()
if success:
# specify directory path
save_directory = './shots/'
filename = f'captured_photo_{int(time.time())}.jpg'

# save
cv2.imwrite(os.path.join(save_directory, filename), frame)

# stop camera
camera.release()

return jsonify({'message': 'Photo captured and saved successfully', 'filename': filename}), 200
else:
return jsonify({'error': 'Failed to capture photo'}), 500
except Exception as e:
return jsonify({'error': f'Error capturing photo: {str(e)}'}), 500

def allowed_file(filename):
"""
Expand Down
3 changes: 2 additions & 1 deletion web-app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pymongo
requests
python-dotenv
pylint
black
black
opencv-python
4 changes: 2 additions & 2 deletions web-app/templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ <h1>Results</h1>
datasets: [{
label: 'Predicted Age',
data: {{ predicted_ages | tojson | safe }},
borderColor: 'rgba(255, 99, 132, 1)',
borderColor: 'rgba(54, 162, 235, 1)',
fill: false
},
{
label: 'Actual Age',
data: {{ actual_ages | tojson | safe }},
borderColor: 'rgba(54, 162, 235, 1)',
borderColor: 'rgba(255, 99, 132, 1)',
fill: false
}]
},
Expand Down
22 changes: 22 additions & 0 deletions web-app/templates/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,31 @@
alert('Error starting task: ' + error.message);
});
}

function capturePhoto() {
fetch('/capture_photo')
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error('Failed to capture photo');
}
})
.then(data => {
alert(`Photo captured and saved successfully: ${data.filename}`);
})
.catch(error => {
console.error('Error capturing photo:', error);
alert('Error capturing photo');
});
}
</script>
</head>
<body>
<div class="container">
<h1>Capture Photo</h1>
<button onclick="capturePhoto()">Capture Photo</button>
</div>
<div class="container">
<h1>Upload Image for Age Estimation</h1>
<form id="uploadForm" onsubmit="event.preventDefault(); startTask();">
Expand Down

0 comments on commit 3e01f4a

Please sign in to comment.