webcam fixes

This commit is contained in:
Ali Abid 2020-11-19 06:32:04 -08:00
parent 095f263fdf
commit acd806dda8
3 changed files with 51 additions and 27 deletions

View File

@ -87,14 +87,18 @@ const image_input = {
dest_height: dim, dest_height: dim,
}) })
Webcam.attach(this.target.find(".webcam_box")[0]); Webcam.attach(this.target.find(".webcam_box")[0]);
io.target.find(".webcam").click(function() { if (io.io_master.config.live) {
Webcam.snap(function(image_data) { io.target.find(".webcam span").hide();
io.target.find(".webcam").hide(); } else {
io.target.find(".image_display").removeClass("hide"); io.target.find(".webcam").click(function() {
io.set_image_data(image_data, /*update_editor=*/true); Webcam.snap(function(image_data) {
io.state = "IMAGE_LOADED"; io.target.find(".webcam").hide();
}); io.target.find(".image_display").removeClass("hide");
}) io.set_image_data(image_data, /*update_editor=*/true);
io.state = "IMAGE_LOADED";
});
})
}
} else if (this.source == "canvas") { } else if (this.source == "canvas") {
io.target.find(".sketchpad").removeClass("hide"); io.target.find(".sketchpad").removeClass("hide");
var dimension = Math.min(this.target.find(".canvas_holder").width(), var dimension = Math.min(this.target.find(".canvas_holder").width(),
@ -156,12 +160,20 @@ const image_input = {
io.io_master.input(io.id, this.image_data); io.io_master.input(io.id, this.image_data);
} }
} else if (this.source == "webcam") { } else if (this.source == "webcam") {
if (!Webcam.loaded) {
io.io_master.no_input();
return;
}
Webcam.snap(function(image_data) { Webcam.snap(function(image_data) {
io.target.find(".webcam").hide(); if (io.io_master.config.live) {
io.target.find(".image_display").removeClass("hide"); io.io_master.input(io.id, image_data);
io.set_image_data(image_data, /*update_editor=*/true); } else {
io.state = "IMAGE_LOADED"; io.target.find(".webcam").hide();
io.io_master.input(io.id, image_data); io.target.find(".image_display").removeClass("hide");
io.set_image_data(image_data, /*update_editor=*/true);
io.state = "IMAGE_LOADED";
io.io_master.input(io.id, image_data);
}
}); });
} else { } else {
io.io_master.no_input(); io.io_master.no_input();

View File

@ -8,7 +8,7 @@ def snap(image):
return np.flipud(image) return np.flipud(image)
iface = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image") iface = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image", live=True)
iface.test_launch() iface.test_launch()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -87,14 +87,18 @@ const image_input = {
dest_height: dim, dest_height: dim,
}) })
Webcam.attach(this.target.find(".webcam_box")[0]); Webcam.attach(this.target.find(".webcam_box")[0]);
io.target.find(".webcam").click(function() { if (io.io_master.config.live) {
Webcam.snap(function(image_data) { io.target.find(".webcam span").hide();
io.target.find(".webcam").hide(); } else {
io.target.find(".image_display").removeClass("hide"); io.target.find(".webcam").click(function() {
io.set_image_data(image_data, /*update_editor=*/true); Webcam.snap(function(image_data) {
io.state = "IMAGE_LOADED"; io.target.find(".webcam").hide();
}); io.target.find(".image_display").removeClass("hide");
}) io.set_image_data(image_data, /*update_editor=*/true);
io.state = "IMAGE_LOADED";
});
})
}
} else if (this.source == "canvas") { } else if (this.source == "canvas") {
io.target.find(".sketchpad").removeClass("hide"); io.target.find(".sketchpad").removeClass("hide");
var dimension = Math.min(this.target.find(".canvas_holder").width(), var dimension = Math.min(this.target.find(".canvas_holder").width(),
@ -156,12 +160,20 @@ const image_input = {
io.io_master.input(io.id, this.image_data); io.io_master.input(io.id, this.image_data);
} }
} else if (this.source == "webcam") { } else if (this.source == "webcam") {
if (!Webcam.loaded) {
io.io_master.no_input();
return;
}
Webcam.snap(function(image_data) { Webcam.snap(function(image_data) {
io.target.find(".webcam").hide(); if (io.io_master.config.live) {
io.target.find(".image_display").removeClass("hide"); io.io_master.input(io.id, image_data);
io.set_image_data(image_data, /*update_editor=*/true); } else {
io.state = "IMAGE_LOADED"; io.target.find(".webcam").hide();
io.io_master.input(io.id, image_data); io.target.find(".image_display").removeClass("hide");
io.set_image_data(image_data, /*update_editor=*/true);
io.state = "IMAGE_LOADED";
io.io_master.input(io.id, image_data);
}
}); });
} else { } else {
io.io_master.no_input(); io.io_master.no_input();