0

I am working on a project related to image processing. Currently, I am taking a image from the webcam and saving it in some folder. Then I display that image as a background image on the div. This works fine. But when I take the image again, the background image still remains the same. But when I see the image in the folder, it has changed.

Note: Both the images are saved by same name. So, previous image is just replaced by current image. And the image is taken by clicking a button.

html code:

 <button id = "click" onclick = "mooddetect()">MOODY</button>
     function mooddetect() {
            $.ajax({
            url: '/mooddetect/',
            type:"POST",
            cache:false,
            success: function(input) {
                var x = document.getElementById('photo');
                x.style.backgroundImage = "url('/static/detect/test.jpg')";
            },
            failure: function(data) { 
                alert('Got an error dude');
            }
        }); 
        }

views.py:

def get_image(camera):
    retval, im = camera.read()
    return im

def webcam():
    camera_port = 0
    ramp_frames = 30
    camera = cv2.VideoCapture(camera_port)
    for i in xrange(ramp_frames):
        temp = get_image(camera)
    print("Taking image...")
    camera_capture = get_image(camera)
    file = "./music/static/detect/test.jpg"
    cv2.imwrite(file, camera_capture)
    del(camera)
@csrf_exempt
def mooddetect(request):
    webcam()
    return HttpResponse("success")
Shivam Mitra
  • 1,040
  • 3
  • 17
  • 33
  • I think the problem is, that the image name dont change (so no refresh). Try to change the background image to a dummy pic (other name) and change it back to the new image (old name). – Cracker0dks Nov 28 '16 at 10:50

2 Answers2

2

UPDATE 2 - check this question for more info and better ways to acheive that: Refresh image with a new one at the same url

UPDATE: What helped was saving the images with a new name and send the updated url to the front, as i assumed it have to do with cache, but im not sure why adding ?c=rand didn't helped.

I assume it will have to do with the cache of the browser.

Try to add timestamp or other rand number to the image url, so you will force to load the new image every time.

x.style.backgroundImage = "url('/static/detect/test.jpg?c=|some random number|')";

you can read more: how to clear or replace a cached image

Community
  • 1
  • 1
Yan Mayatskiy
  • 353
  • 3
  • 12
-1

Try this to clear the cache:

x.style.backgroundImage = `url('/static/detect/test.jpg?${Date.now()}')`
Ma Ni Sh
  • 1
  • 1