How do I draw received png data from ajax call and insert into canvas?
Video file (mp4) is uploaded via ajax to flask server. First frame is extracted with Cv2, saved and the file returned to the client.
I can get png data as string, but want to display image in a canvas.
I can’t inject the code by using image.src = “data:image/PNG;base64″+data. Unsure the source of the error that occurs.
EDIT
I am able to pass image data to an img element, it only shows an icon and not the image itself.
flask functions – main
@app.route('/',methods=['GET','POST'])
def request_from_main():
if request.method == 'POST' and 'file' in request.files:
file_object = request.files['file']
secure_file_object = secure_filename(file_object.filename)
file_object.save(f"uploads/{secure_file_object}")
video_container = video_processing.process_saved_video_file(f"uploads/{secure_file_object}")
saved_image_file_path,saved_image_file_name = video_container.save_image()
return send_file(saved_image_file_path,mimetype='image/png',as_attachment=True)
#return video_container.base_64_encode()
elif request.method == 'GET':
return render_template('index.html')
else:
return None
flask function – cv2 save and provide file path
def save_image(self): # TODO add error checking for image
cv2.imwrite(f"{self.file_structure}/{self.base_file_name}.png",self.current_image)
return f"{self.file_structure}/{self.base_file_name}.png", f"{self.base_file_name}.png"
Client – JavaScript
<!-- language: lang-js -->
// Upload class
var Upload = function (file) {
this.file = file;
};
Upload.prototype.getName = function() {
return this.file.name;
};
Upload.prototype.doUpload = function () {
var that = this;
var formData = new FormData();
// add assoc key values, this will be posts values
formData.append("file", this.file, this.getName());
formData.append("upload_file", true);
// Change url according to flask design
$.ajax({
type: "POST",
url: "/",
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', that.progressHandling, false);
}
return myXhr;
},
success: function (data) {
console.log("SUCCESS, pushing to canvas");
console.log(typeof data);
$("#temp_storage_image").attr("src",data);
},
error: function (error) {
console.log("ERROR")
console.log(error);
},
async: true,
data: formData, // What to send
cache: false,
contentType: 'image/png',
//processData: false,
dataType: 'image/png', // What to expect back from the server, must match otherwise error is thrown,
timeout: 100000
});
};
//Change id to your id
$("#file_upload_input").on("change", function (e) {
var file = $(this)[0].files[0];
console.log("Made it here, to on change file upload");
var upload = new Upload(file);
console.log("Made it past new Upload");
// maby check size or type here with upload.getSize() and upload.getType()
// execute upload
upload.doUpload();
});
Client – HTML
<body>
<div class="container">
<div id="project_structure_menu">
<form id="file_upload" idmethod="POST" enctype="multipart/form-data" action="{{url_for('request_from_main')}}"> <!-- request_from_main is flask function -->
<div class="input-field col-sm">
<input id="file_upload_input" type="file" name="file">
</div>
<div class="input-field col-sm">
<button id="file_upload_submit_button" type="submit">Submit</button>
</div>
</form>
<div id="progress-wrp" class="col s2">
<div class="progress-bar"></div>
<div class="status">0%</div>
</div>
</div>
<div id="labeling_ui" class="row">
<img id="temp_storage_image"></img>
<canvas id="main_drawing" width="100" height="100" style="border:1px solid #d3d3d3;"></canvas>
</div>
</div>
<!-- TODO Make own file: Get form data and send it to server -->
<script type="text/javascript" src="{{url_for('static',filename='scripts/file_upload.js')}}"></script> <!-- AJAX file upload -->
</body>
2
Answers
Solved it! My lawd.
How to send a video file through ajax, process video and return first frame in an img tag
This solution sends identifier string to collect file through get request. Unsure of how secure this solution is.
Flask Code
Video Processing
JavaScript - AJAX - jQuery
Index - HTML
(1) Flask server image recv
(2) jQuery canvas image send