 Welcome back everyone today. We're going to be talking about problem solving using programming. Please like and subscribe to help this channel grow today, we're going to be talking about finding or fixing the problem of detecting strobe effect now if you've never seen a strobe effect gift they switch really fast usually are very bright colors and things like that so It seems like a pretty interesting problem to try to solve can we detect? strobing gifts and I thought it would be actually a little bit easier than Than it was but I'll go through how I went about trying to solve this problem because this is Essentially how I go about trying to solve most problems. So I do have a gift strobe detect Script that I wrote in just bashell and Linux. I'll talk about that in a second What I want to show you right now, however, is in the data folder I have a couple different gifts and the ones with one that say one dash strobe are actually Strobe effect gifts and you can already see that they are very usually very colorful or they change really quickly and then zero are Ones that are not strobing gifts or what I classify as not strobing gifts And I have the test jpegs and these are non moving non moving images So you can see here. These are just jpegs of the gifts themselves and I've kind of pulled them all out using image magic With the convert feature. So if you convert a gift using image magic and the convert Command then it will turn all of the gifts Can I say frames into Individual jpeg images so you can see the first one is a cat for probably like less than three seconds And then a cat far away for two seconds. So these change So quickly that it can it kind of has a strobe effect to it another example is are these orange pink orange blue Boxes this is a real strobe effect where one of the boxes inside is getting bigger over time But they're very loud colors that keep changing every single frame. So there's a huge difference Between each frames of that colorful gift and then there's one at the bottom, which is actually no colors It's just black and white the problem here is that it's changing between a dark color and a white color So exactly inverted colors and if you do that really fast, that's also a strobe effect So I'm going to talk about these images and what I was doing to try to detect strobe effect on them so Starting out we let's say we have our test data set and here That's three gift images that do not have a strobe effect And then three gift images that do have a strobe effect if we were actually making a real program out of this you would want a lot more samples than this but these are three samples to show you at least how the sampling works and Each of these are different enough that they're gonna have each each of them will have their own different problem sets Let's say what we want to do first before we actually get into the code. Let's make a hypothesis. So we want to try to detect a strobing gift Completely based on some quality that we can get out of it. So what do we know about strobing gifts? What hypothesis can we come up with? Well one hypothesis is that The colors will change quickly. So for example, the colors will change quickly If we can detect that the colors are changing very rapidly like every whenever I say quickly, I should probably say every few frames like dramatic dramatic color change dramatic color change Every few frames. So like maybe there's one or two frames in between but basically one or two frames and then a Drastic color change for one or two frames and then a drastic color change So this is what I would expect from some sort of strobing thing if you think of it like strobing is Essentially dark and then light and then dark and then light and then dark and then light Well gifts might have a little bit more time in between that so it might be dark dark light dark dark light something like that but what we're actually trying to detect is this change between either two colors or two Can I say light lightnesses like dark or light? So let's see what we can do here. So based on this hypothesis. This is one of the problems That I want to solve is how do we detect it? I? suspect that there's a dramatic color change every few frames But I'm not exactly sure how that color change happens because I don't know enough about gifts yet Okay, and this is the first hypothesis I went with you could make like right now I would probably make several more hypotheses, but I want to get into the code, okay? So first make your guess about how you would actually detect it And then we want to find features that either support or deny this hypothesis Okay, so let's save that dramatic colors change every few frames. Okay, so I have my data set and You can see I've already labeled them so anything with a zero or not strobe anything with a one is a strobe And the reason that I already labeled these is because I want to be very clear about my data set and what How I would classify them so I've already pre manually pre classified them and now that they're labeled I could probably if I can figure out which features I'm interested in I could throw them into a classifier so I can classify for example strobe effect gifts and Non-strobe gifts based on some sort of artificial intelligence, but we're not going to get into that today I'm just going to look at the features that I'm interested in Okay, so I've already pre classified them so I know which ones I'm actually looking at now The first thing we have to do is find the features that we're actually interested in well How can I find features for images every time I think of images? or finding information about images I always think of Image magic. It's kind of a I can't say default, but it's a very common tool Used in Linux environments. You can get it for Windows OS X like image magic is just a very common tool And it's very useful for doing things like I said before like conversions, but it also has tools for identifying information so specifically on Linux the tool in image magic to find information about images is the identify Tool so if we do identify and then let's say zero head turn Then I can see a couple different things here and this first identify Doesn't look like it's telling me very much Nothing nothing that I would be interested in in terms of my hypothesis remember going back my hypothesis is My hypothesis is there are dramatic color change every few frames, right? So I'm looking actually for some way to identify the colors inside the gif This is why it's important to have a hypothesis So you can always look back and make sure that you're on the right track if I look at identify head turn I don't see really any color information except sRGB. So I know that it is sRGB And I might be able to use that in the future, but I don't see anything else that's color related. Okay now identify Has another feature or another let's say More information so I can use identify dash verbose and then Give it head turn again and then hit enter and this is a lot of information so Instead of doing it all at once. I'm going to clear that and then let's throw that into pipe that into more So what I'm doing here is identify dash verbose The file that I'm interested in that's not Strobing effect and then this up and down bar is a pipe So I'm piping everything from this command into the more command and what the more command does is just let me View it kind of like pages so it doesn't just scroll through really quickly. I can see it in terms of pages Okay, so now we have The first part and you can see at the bottom it's more so now if I hit space bar I can just go one page at a time. So let's see what we have here with the verbose output. So I have the format mime type Geometry color space sRGB, which we already knew depth is 8-bit. I don't really see anything Too interesting yet here. I have some channel statistics and they show red now This actually could be interesting so I have red and next I can see some green and the red min is 22 red max 211 and the mean is 107. Okay, so That actually might be interesting or something I could use where we detect for example We see red and then we find the next mean red value or something like that Okay, just guessing now. I'm just looking what features might be available green has a mean of 91 Blue has a mean of 75. Okay, and this is for the first frame. This is for the first frame of the gift Alpha looks like 255 all the way around. Okay, so it looks like I might be able to do something with that blue green red green blue Maybe in the image statistics overall We have histogram. Okay, so this actually could be pretty interesting We have the histogram of the image itself, but I don't know if this is just the histogram of the first image or not I bet it's for the first image and we have sRGBA and Here are the color values so we can see What red green blue and red is getting more, but it's not actually getting more over time Okay, another histogram Okay, and then we have Again red primary green primary blue primary Here we have some background color so sRGBA 255 255 255 So everything is on Border color doesn't look that interesting transparent color also looks 255 255 255 so also not very interesting So what I'm trying to do is go through here and find anything that I can possibly use to Try to detect Some change between the different frames now. We're on a different frame With the next frame I should say and then we have our mean and the mean I believe for red went down Green went up a little bit, but basically stayed the same and then Blue looks like it stayed the same as well So here I'm not saying very much differentiation. I could probably use this histogram a little bit more I'm not seeing much differentiation between the different frames Those also look about the same Which means There's not much change between them and that's actually what we would expect So I'm gonna close that out That's what we would expect because we were dealing with a non a non Strobing gif. Okay, so now let's look at a strobing gif and specifically one that I know We have a feature for colors Okay, so here I'm looking at identify dash verbose One dash strobe dash colors dot gif and then piping that into more and then looking what we have here So notice we have basically all of the same features that we had in the non strobe gif and Then we have a mean in the red that's really high so 254 red and Then green mean 90 that was about like the other one and then blue we have a mean of zero Okay, so this actually might be useful here We have red that's very high and a blue that's very low and then we also have the histogram again We have the red primary green primary Background color is now not just 255 255. We actually have some values in the background color same for Not quite the same for the border same for transparent color. So Here we have what is that red is? Zero So we have zero here 174 and then 217 so this is almost max out with a with 255 at the At the top so this is the first frame and it looks like background color and transparent color Could be an interesting feature. So let's go look at the the next one. Just remember zero 174 and then 217 So let's go look at The next one. So now we're in the next one, but we're looking for the transparency or background color That histogram again could be useful for us But we want to try to find like one specific feature. That's We can use for reliable detection rather than many features if possible So going back to background color, you still have zero one seventy four two seventeen and then transparent color two five five 90 zero so here from the first frame We had zero on red in terms of transparency and then now we have two five five on red So basically it went from nothing to Maximum and then on the what is that blue probably on the blue We had almost maximum to now nothing. So that actually looks like a strobe So from one frame to the other we have a complete reversal of colors if that pattern continues then It could be very interesting for us to do detection So let's get out of that and I'm just going to try to filter for Transparent culture color because that looks like it's interesting. So instead of running this through more I'm going to I'm gonna run this through grep and grep is a tool for filtering Built into Linux and I believe OS X and there is a grep for Windows if you want to download it and use it from command line So we have grep and then I want to search for the term transparent Color So let's run that and see what happens. So now we're getting out all the transparencies And there's quite a lot of them. Okay, so you can see here. You can actually hopefully you can understand this as a Really good indicator here We have zero in the red and then we have two five five and then we have zero and then we have two five five zero Two five five zero two five five and we have two five five for a little bit and then zero and then two five five So what's happening here is? What we would expect if we're seeing some sort of strobe where the colors are changing back and forth the red is Changing a lot. Let's see about the blue. So zero and then two nineteen zero two thirty one zero two Thirty one zero two two Zero two thirty one. Yeah, so basically it looks like it's Switching between red and blue in terms of maximum values. So this looks like a pretty good indicator now What is this indicator telling us? How can we actually use this indicator? Well, what's happening here? We expect a strobe and and this is where you would actually have to kind of fine-tune what you're trying to do but we expect a strobe whenever we see a very big jump in a particular color and In this case, it looks like a very big jump in the reverse direction for another color. So we have Basically red going from zero to two fifty five and then blue going from zero to two thirty or something around that so I would say if you have a big jump around like Hundred and fifty or two hundred And you see that big jump going up and down Multiple times then that could be considered a strobe effect. You basically can detect it after two jumps So if we go from zero to two fifty five and then back to zero and then back to two fifty five I would consider that a strobe now if it's just one time. I probably wouldn't consider it a strobe so Because you know, you might just have a color change in the image But it's not actually trying to do a strobe effect. There's just one color change and then that's a solid color from from then on But if you're actually going back and forth, it's probably an indication of a strobe effect. So we can then use that Okay, so From this I wrote a script Let's see what we got here. So I wrote a bash script that just uses identify from image magic And detects this big change in red or blue Okay, specifically both together because we want to know is one going up another and the other is going down And then one's going the other is going up and the other is going down That's what we want to find out. Okay, so here we have at the beginning of the this bash script I have been sh to run the Shell script and then a little comment about when it was written The first thing we're doing is checking that we're actually entering a file I don't check what type of file it is. I just check that it is a file, but you could check if it's a gif or not And then if it doesn't then please give a path to a valid file and then we probably actually need to exit so Let's do exit Okay, so we're going to exit and we're going to give another error code like 255 I don't want to exit with zero or one because if it's a zero it exited without detecting if it's a one It exited with detecting the the strobing gif So I need to give it a different code and then we check to see if image magic is installed So specifically for linux. I'm looking for the identify command so Let's go over here And you can find out where a tool is installed or if it's installed by using the which command so which identify Okay, so which identify and you see that I have identified installed in user ben identify so I can do which fdk imager And I have it installed also in the same directory user ben fdk imager. Okay, so you can use that Which taco and then if there's no tool installed with or no binary installed with that That name then it will show nothing and then if we echo The value you get a one so which identify Echo Dollar sign question mark says whether that was a successful command or not and you get a zero so success On the which command is a zero Failure is a one. Okay, so this echo dollar sign Question mark is just the return value of the last command that you ran. So Right, so now we know where identify is located. So which identify User ben identify. Okay, so we know that it's installed And then basically here we check that Identify is actually installed and then if it's not installed then we also exit with a 255 error If it is installed then we just echo or print The location That we found it at say that we did find it. Okay, then the next thing is check the strobe counter So I have a function here called check strobe c and strobe c is just a strobe counter I'm taking in a variable or an argument to the function and then we have check strobe and dollar sign one Is the counter itself I can just say that And then if strobe c is equal to two Then the image seems to have some strobe effect and this is what I was talking about Whenever we were looking at the features Let's go back to Transparent color color features So if we detect one major change, we don't say it's a strobe But if we're detecting two major changes in the color, then we say it is a strobe Okay So here is where we're actually setting how many how many detections do we need to have to consider it a strobe Image and if it says exit if it's greater than Two, okay, so if we detect two major changes, then Forget about it or we're done and we think that this is a strobing image Okay, and then because it's a strobing image we exit with one which means we've detected the strobing image all right Now we have another function Called check transparency strobe now The reason I say check transparency strobe strobe is because we're using the transparent called color feature To detect transparency, but we could very easily use what was it called background background color? background color now you notice here this Gif does not change its background color. So this isn't really a very good feature to use for this particular Gif, but it might actually be a useful feature for other gifts Okay So here I have one function checking the transparency strobe and that would detect the first one based on the transparent transparency color feature We could write of another function very easily that's check background strobe for example If if we don't detect anything in transparency scroll strobe then Detect the background feature Okay, then we have our counter. So we're initializing strobe c which is the strobe counter And then it's if that's the counter that's over two then we classify it as a strobe and we exit We preset our red green and blue values to zero just to initialize them These are variables. So rgb here is just a variable Initialized to zero and we have a for loop. So for line in This command. So we're actually giving a command and then we are Taking each line from that command and then processing it somehow and what that does is Let's go back over What that does is in our transparency color We'll detect each of these lines and then process each of these lines in a row So we'll process each of these lines In a row Okay, so then for each one of these lines take one line get the values and then move to the next line Okay, so here we're getting all of the values for transparency from This line actually I could probably make this a little bit shorter And then what we're doing here is using grep and grep to do filtering on the keywords and then awk to print What we actually want to What features we want to extract so let's go ahead and do this Let's do this. Let's pipe this into awk and then I think All right dollar sign three And what this is doing is awk by default will use spaces So this will remove transparent space color and just give me s r gba and then the values Okay, so now I have s r gba and then the values and then what I want to do is remove s r gba s r gba and I want to remove this last Bracket so if we go back over I'm removing the last brackets with said and if we do s Basically said s r gba back slash bracket slash slash. This basically says replace this value Sorry replace this bracket and s r gba with nothing So replace s r gba with nothing and then also said replace the last bracket with nothing So said is really handy To remove things and I could actually combine these if I was Not so lazy. Okay Okay, so now that we have the line what we end up getting from this is each individual line With their individual values Okay So what I want to do here I'm echoing the line and I'm using awk And splitting them at the comma And then I'm printing either the first value second value or third value and then saving that into red to green to and beat Bluetooth variables. So what that will do is give me Either the value 255 for red Green is 90 and then blue is zero and then that's set into the Second variables here Then we actually print out. What was the case last time? So our prior values are just r gb and our current values are r2 G2 and then bluetooth and then this is where we start doing our comparison. Okay so Now we go down we describe how we're actually doing our detections here and our first detection is Just comparing the values because they're numeric. It's relatively easy to compare here We have r2. So if r2 the value currently in r2 is greater than r plus 150 if we've had an increase in the r value more than 150 then sorry and bluetooth is less than The first blue minus 150. So we've had this complete inverse in Green or sorry red and blue If we've had a complete inverse where blue went down and red went up more than 150 Then we have an increase in red detected Okay, and then we increment The strobe counter and then we check strobe to see if it's greater than two or not If that's not true if we didn't have an increase or decrease greater than 150 Then we check to see if r2 is less than r minus 150 whether r has went down the red value has went down And the blue value has gone up Okay If the r value has gone down and the blue value has gone up more than 150 Then we say there's an increase in blue detected and Then we increment our strobe counter And then we check to see if the strobe counter is over two if it is over two then we exit Okay, now what this is doing Is just figuring out how much Has the color value actually changed so if we see these changes a lot Or if we see both sides going up and down continuously Then we we suspect it's a strobe now after two After two times of that I'm saying I've already detected this major change twice So I think it's probably a strobe. So I'm going to go ahead and say it's a strobing gift But if you wanted to be more accurate, maybe you want to put it to like three or four times But then again, some gifts are very very short. Maybe even there's only Actually two Two changes would probably be the the the shortest value you could you could make it because if it's just one You only have one image. Okay And then we set the values the colors for the next time for the next round of check So here we have red green blue and we set them to the value of r2 g2 and b2 Because we want to loop again And then we want to compare the prior loop to the next loop and then see if that's also strobing And then basically do everything again until we get two strobes Okay And then we check This check transparency strobe is actually where we come into the program. So this is would be our kind of like our main So the other two The other two things were just functions and functions are something that we call So here i'm in my kind of main. Let's say my main function This will actually run first and then I will call the functions up above with the with the image that The user gave us If we make it through all of this and we never exit then no strobe was detected We already ran all of our checks through all of the different Values or features that we had we didn't see any strobe So we exit zero which means that we exited successfully or we didn't find Any strobe value. Okay, so it's a fairly straightforward Script we basically just get some feature that we're interested in and then we compare that feature over the life of the gif or over each Frame of the gif. Okay now The thing is this is only checking for transparency because we're filtering out everything else So if your your gif is strobing on transparency This will probably detect it if your gif is strobing on background image This will not check it because we're not checking the background feature. So this is why Gif detection is a little bit more difficult than I expected It looks like there's a lot of different ways to detect Or sorry, it looks like there's a lot of different ways to check Uh For strobing or make strobing happen in the gif depending on how the gif was created. Okay, so now let's go ahead and try this We have Our strobe detector and we have our data inside our data folder So we'll just run dot slash strobe tester And then I'm going to give it first a a gif that I know is not Shouldn't be detected. So let's do rude Okay, so here we have checking file data slash zero Rude dot gif and then identify command found at user ben identify that was using the witch command And then we have no strobe detected. So let's do another one That shouldn't be detected just to make sure So jerk Okay, no strobe detected. So that's what we expect. Okay. Now. Let's do the ones that we know are strobe images So strobe images data and then I'll just do What's the first one cat Strobe cat No strobe detected. So we have a cat No, strobe was detected and I'm pretty sure I know the reason because There's only one major change and I don't even know if that's using transparency. We can check that in a second Here, let's do the next one, which is colors and this is what I actually expect Okay, so this is the one we found transparency on So now, yep, it went into our loop. We're printing the prior, which is zero This is the kind of initialized prior and we have a current which is changing a lot Actually, I just realized setting the prior values to zero Might actually increment our counter early. So I might need to check that So we have our blues at two seventeen From zero that would probably be one increment and we have our prior the next current is red Five and blue zero. So here we have an increase in red detected and it looks like the counter is still okay Yep, and then we have some more frames that didn't really change and then we have another big change So increase in blue detected and the check strobe got to two. So the image Seems to have a strobe effect only based on the transparency feature. Okay So then Let's go one more and this one I know is not going to work Because it's not using transparency. So this is dark light. This one didn't actually have colors. It was the black and white one I don't think it was using transparency for it. Yeah, so no strobe detected. So there are three strobing Gifts the problem is only one of them is using transparency to do the strobe effect. The other ones are using Something else. I'm not sure yet. I haven't really looked at them But I think one of them is using background Which is another function we need to put in and then one of them I don't really know yet So the ideal situation is that we could actually pre-process all of these images first So that way we can look at one feature. So If there was a way to flatten all of the gif images or make them, uh, you know, some sort of uniform rgb Value if we can do that first pre-process them and then use our detection algorithm To find out if they're strobing that would be the best case Um, so I'm still thinking about what that would be But I'm pretty sure we can do that if we just convert them all to kind of a simplified version basically of gif so The whole point of this is um to figure out What uh, your goal is so here our hypothesis was Dramatic color changes Every few frames can be used to detect Um, uh strobing gifs. Okay, so dramatic color changes every few frames. That is true At least for the transparency We've shown that that is possible, right So we can extract the transparency colors color features and then use them to classify At least one, uh one type of gif That way so here we came up with the hypothesis and then because of that hypothesis That's what I focused on was just these dramatic color changes So think about any other hypothesis you can you can think of we have two more gifs Where this didn't actually work on the transparency. So what else can we do? What else can we look for? For these gifts to be able to detect them as strobing gifts and that's something that I'll leave to you guys So put your hypothesis in the comments below. Um, if you want to you can, um Contribute to this project. I'll put the link to the github code Um, uh also in the link below. So tell me what your hypothesis are for detecting the other images Um, uh other strobing gifts and then um, go ahead and look at github If you if you're not familiar with github yet Take a look at it and see if you can actually detect either One of these other gifts or maybe find your own gif and try to do detection So this is a an interesting little problem It wasn't very hard to do the whole point is we just have to find some good features to be able to Do some basic detection. Hopefully eventually we can find a way to do general strobe detection on all gifts That way we can make a useful filter For these these big companies or web portals that way People won't get socially engineered or attacked again So that's it for today. I just wanted to show you how i'm going through Kind of this problem solving If we're presented with a problem, can we make a solution? Yeah, most solutions are are actually fairly simple to do It's just people don't really work through them or they don't think about what the possible solutions could be first So I hope that was helpful and that's it for today. Thank you very much Thanks for watching. If this was helpful, please like and subscribe Also, please consider supporting us on patreon. Your support lets us focus on making better tutorials for everyone