<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akash Bhandari on Medium]]></title>
        <description><![CDATA[Stories by Akash Bhandari on Medium]]></description>
        <link>https://medium.com/@akashbhandari?source=rss-1f237cb9f568------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HzADSNgnfPFEYnYdCg0zVA.jpeg</url>
            <title>Stories by Akash Bhandari on Medium</title>
            <link>https://medium.com/@akashbhandari?source=rss-1f237cb9f568------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 04:38:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akashbhandari/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Tic Tac Toe Python Game Project — “Memory of school time”]]></title>
            <link>https://medium.com/analytics-vidhya/tic-tac-toe-python-game-project-memory-of-school-time-be0e1ad55c66?source=rss-1f237cb9f568------2</link>
            <guid isPermaLink="false">https://medium.com/p/be0e1ad55c66</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tic-tac-toe]]></category>
            <category><![CDATA[pygame]]></category>
            <dc:creator><![CDATA[Akash Bhandari]]></dc:creator>
            <pubDate>Tue, 08 Sep 2020 09:15:22 GMT</pubDate>
            <atom:updated>2020-09-16T04:35:16.015Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*EPh5Ucw8SxMRxOKZ5Tgaxg.png" /><figcaption><strong>‘Let’s Play Tic Tac Toe “XO” All you have to do is to match the two hearts to win a heart But you might lose if I matched those hearts’</strong></figcaption></figure><h3>Tic Tac Toe Python Game Project — “Memory of school time”</h3><h4><strong>Story behind this</strong></h4><p>It’s no doubt, you must have played Tic Tac Toe in your school days and every one of us loves to play the game. You will be surprised to know that the game of Tic Tac Toe is known to exist since ancient Egypt times.</p><p><strong>Tic-tac-toe</strong>, <strong>noughts, and crosses</strong>, or <strong>Xs and Os</strong> is a <a href="https://en.wikipedia.org/wiki/Paper-and-pencil_game">paper-and-pencil game</a> for two players, <em>X</em> and <em>O</em>, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner</p><p>Because of the simplicity of tic-tac-toe, it is often used as a <a href="https://en.wikipedia.org/wiki/Pedagogical">pedagogical</a> tool for teaching the concepts of good <a href="https://en.wikipedia.org/wiki/Sportsmanship">sportsmanship</a> and the branch of <a href="https://en.wikipedia.org/wiki/Artificial_intelligence">artificial intelligence</a> that deals with the searching of <a href="https://en.wikipedia.org/wiki/Game_tree">game trees</a>.</p><p>we are going to build an interactive game of Tic Tac Toe where we’ll learn new things along the way.</p><h3>Tic Tac Toe — About the Python Project</h3><p>The interesting Python project will be build using the pygame library.</p><p><strong>Pygame</strong> is a set of <a href="http://www.python.org/">Python</a> modules designed for writing video games. Pygame adds functionality on top of the excellent <a href="http://www.libsdl.org/">SDL</a> library. This allows you to create fully featured games and multimedia programs in the python language.</p><p>Pygame is highly portable and runs on nearly every platform and operating system.</p><p>We will be explaining all the pygame object methods that are used in this project. Pygame is a great library that will allow us to create the window and draw images and shapes on the window. This way we will capture mouse coordinates and identify the block where we need to mark ‘X’ or ‘O’. Then we will check if the user wins the game or not</p><h3>Prerequisites</h3><p>To implement this game, we will use the basic concepts of Python and Pygame which is a Python library for building cross-platform games. It contains the modules needed for computer graphics and sound libraries. To install the library, you can use pip installer from the command line:</p><ol><li>pip install pygame</li></ol><h3>Steps to Build a Python Project — Tic Tac Toe Game</h3><p>First, let’s check the steps to build Tic Tac Toe program in Python:</p><ul><li>Create the display window for our game.</li><li>Draw the grid on the canvas where we will play Tic Tac Toe.</li><li>Draw the status bar below the canvas to show which player’s turn is it and who wins the game.</li><li>When someone wins the game or the game is a draw then we reset the game.</li></ul><p>We need to run our game inside an infinite loop. It will continuously look for events and when a user presses the mouse button on the grid we will first get the X and Y coordinates of the mouse. Then we will check which square the user has clicked. Then we will draw the appropriate ‘X’ or ‘O’ image on the canvas. So that is basically what we will do in this Python project idea.</p><h3>1. Initializing game components</h3><p>So let’s start by importing the pygame library and the time library because we will use the time.sleep() method to pause game at certain positions. Then we initialize all the global variables that we will use in our Tic Tac Toe game.</p><blockquote>import pygame as pg,sys</blockquote><blockquote>from pygame.locals import *</blockquote><blockquote>import time</blockquote><blockquote>#initialize global variables</blockquote><blockquote>XO = ‘x’</blockquote><blockquote>winner = None</blockquote><blockquote>draw = False</blockquote><blockquote>width = 400</blockquote><blockquote>height = 400</blockquote><blockquote>white = (255, 255, 255)</blockquote><blockquote>line_color = (10,10,10)</blockquote><blockquote>#TicTacToe 3x3 board</blockquote><blockquote>TTT = [[None]*3,[None]*3,[None]*3]</blockquote><p>Here, the TTT is the main 3×3 Tic Tac Toe board and at first, it will have 9 None values. The height and width of the canvas where we will play the game is 400×400.</p><h3>Initializing Pygame window</h3><p>We use the pygame to create a new window where we’ll play our Tic Tac Toe game. So we initialize the pygame with pg.init() method and the window display is set with a width of 400 and a height of 500. We have reserved 100-pixel space for displaying the status of the game.</p><p>The pg.display.set_mode() initializes the display and we reference it with the screen variable. This screen variable will be used whenever we want to draw something on the display.</p><p>The pg.display.set_caption the method is used to set a name that will appear at the top of the display window.</p><blockquote>#initializing pygame window</blockquote><blockquote>pg.<strong>init</strong>()</blockquote><blockquote>fps = 30</blockquote><blockquote>CLOCK = pg.time.<strong>Clock</strong>()</blockquote><blockquote>screen = pg.display.<strong>set_mode</strong>((width, height+100),0,32)</blockquote><blockquote>pg.display.<strong>set_caption</strong>(“Tic Tac Toe”)</blockquote><h3>3. Load and transform images</h3><p>The Python machine learning project uses many images like the opening image that will display when the game starts or resets. The X and O images that we will draw when the user clicks on the grid. We load all the images and resize them so that they will fit easily in our window.</p><h3>4. Define the functions</h3><p>Now we create a function that will start the game. We will also use this function when we want to restart the game. In pygame, the blit() function is used on the surface to draw an image on top of another image.</p><p>So we draw the opening image and after drawing, we always need to update the display with pg.display.update(). When the opening image is drawn, we wait for 1 second using time.sleep(1) and fill the screen with white color.</p><p>Next, we draw 2 vertical and horizontal lines on the white background to make the 3×3 grid. In the end, we call the draw_status() function</p><blockquote>def game_opening():<br> screen.blit(opening,(0,0))<br> pg.display.update()<br> time.sleep(1)<br> screen.fill(white)<br> <br> # Drawing vertical lines<br> pg.draw.line(screen,line_color,(width/3,0),(width/3, height),7)<br> pg.draw.line(screen,line_color,(width/3*2,0),(width/3*2, height),7)<br> # Drawing horizontal lines<br> pg.draw.line(screen,line_color,(0,height/3),(width, height/3),7)<br> pg.draw.line(screen,line_color,(0,height/3*2),(width, height/3*2),7)<br> draw_status()</blockquote><p>The draw_status() function draws a black rectangle where we update the status of the game show which player’s turn is it and whether the game ends or draws.</p><blockquote>def <strong>draw_status</strong>():</blockquote><blockquote>global draw</blockquote><blockquote>if winner is None:</blockquote><blockquote>message = XO.<strong>upper</strong>() + “‘s Turn”</blockquote><blockquote>else:</blockquote><blockquote>message = winner.<strong>upper</strong>() + “ won!”</blockquote><blockquote>if draw:</blockquote><blockquote>message = ‘Game Draw!’</blockquote><blockquote>font = pg.font.<strong>Font</strong>(None, 30)</blockquote><blockquote>text = font.<strong>render</strong>(message, 1, (255, 255, 255))</blockquote><blockquote># copy the rendered message onto the board</blockquote><blockquote>screen.<strong>fill</strong> ((0, 0, 0), (0, 400, 500, 100))</blockquote><blockquote>text_rect = text.<strong>get_rect</strong>(center=(width/2, 500–50))</blockquote><blockquote>screen.<strong>blit</strong>(text, text_rect)</blockquote><blockquote>pg.display.<strong>update</strong>()</blockquote><p>The check_win() function checks the Tic Tac Toe board to see all the marks of ‘X’ and ‘O’. It calculates whether a player has won the game or not. They can either win when the player has marked 3 consecutive marks in a row, column or diagonally. This function is called every time when we draw a mark ‘X’ or ‘O’ on the board.</p><blockquote>def <strong>check_win</strong>():</blockquote><blockquote>global TTT, winner,draw</blockquote><blockquote># check for winning rows</blockquote><blockquote>for row in <strong>range</strong> (0,3):</blockquote><blockquote><strong>if</strong> ((TTT [row][0] == TTT[row][1] == TTT[row][2]) <strong>and</strong>(TTT [row][0] is not None)):</blockquote><blockquote># this row won</blockquote><blockquote>winner = TTT[row][0]</blockquote><blockquote>pg.draw.<strong>line</strong>(screen, (250,0,0), (0, (row + 1)*height/3 -height/6),\</blockquote><blockquote>(width, (row + 1)*height/3 — height/6 ), 4)</blockquote><blockquote>break</blockquote><blockquote># check for winning columns</blockquote><blockquote>for col in <strong>range</strong> (0, 3):</blockquote><blockquote><strong>if</strong> (TTT[0][col] == TTT[1][col] == TTT[2][col]) <strong>and</strong> (TTT[0][col] is not None):</blockquote><blockquote># this column won</blockquote><blockquote>winner = TTT[0][col]</blockquote><blockquote>#draw winning line</blockquote><blockquote>pg.draw.<strong>line</strong> (screen, (250,0,0),((col + 1)* width/3 — width/6, 0),\</blockquote><blockquote>((col + 1)* width/3 — width/6, height), 4)</blockquote><blockquote>break</blockquote><blockquote># check for diagonal winners</blockquote><blockquote><strong>if</strong> (TTT[0][0] == TTT[1][1] == TTT[2][2]) <strong>and</strong> (TTT[0][0] is not None):</blockquote><blockquote># game won diagonally left to right</blockquote><blockquote>winner = TTT[0][0]</blockquote><blockquote>pg.draw.<strong>line</strong> (screen, (250,70,70), (50, 50), (350, 350), 4)</blockquote><blockquote><strong>if</strong> (TTT[0][2] == TTT[1][1] == TTT[2][0]) <strong>and</strong> (TTT[0][2] is not None):</blockquote><blockquote># game won diagonally right to left</blockquote><blockquote>winner = TTT[0][2]</blockquote><blockquote>pg.draw.<strong>line</strong> (screen, (250,70,70), (350, 50), (50, 350), 4)</blockquote><blockquote><strong>if</strong>(<strong>all</strong>([<strong>all</strong>(row) for row in TTT]) and winner is None ):</blockquote><blockquote>draw = True</blockquote><blockquote><strong>draw_status</strong>()</blockquote><p>The drawXO(row, col) function takes the row and column where the mouse is clicked and then it draws the ‘X’ or ‘O’ mark. We calculate the x and y coordinates of the starting point from where we’ll draw the png image of the mark.</p><blockquote>def <strong>drawXO</strong>(row,col):</blockquote><blockquote>global TTT,XO</blockquote><blockquote>if row==1:</blockquote><blockquote>posx = 30</blockquote><blockquote>if row==2:</blockquote><blockquote>posx = width/3 + 30</blockquote><blockquote>if row==3:</blockquote><blockquote>posx = width/3*2 + 30</blockquote><blockquote>if col==1:</blockquote><blockquote>posy = 30</blockquote><blockquote>if col==2:</blockquote><blockquote>posy = height/3 + 30</blockquote><blockquote>if col==3:</blockquote><blockquote>posy = height/3*2 + 30</blockquote><blockquote>TTT[row-1][col-1] = XO</blockquote><blockquote><strong>if</strong>(XO == ‘x’):</blockquote><blockquote>screen.<strong>blit</strong>(x_img,(posy,posx))</blockquote><blockquote>XO= ‘o’</blockquote><blockquote>else:</blockquote><blockquote>screen.<strong>blit</strong>(o_img,(posy,posx))</blockquote><blockquote>XO= ‘x’</blockquote><blockquote>pg.display.<strong>update</strong>()</blockquote><blockquote>#print(posx,posy)</blockquote><blockquote>#print(TTT)</blockquote><p>The userClick() function is triggered every time the user presses the mouse button.</p><p>When the user clicks the mouse, we first take the x and y coordinates of where the mouse is clicked on the display window and then if that place is not occupied we draw the ‘XO’ on the canvas. We also check if the player wins or not after drawing ‘XO’ on the board.</p><blockquote>def <strong>userClick</strong>():</blockquote><blockquote>#get coordinates of mouse click</blockquote><blockquote>x,y = pg.mouse.<strong>get_pos</strong>()</blockquote><blockquote>#get column of mouse click (1–3)</blockquote><blockquote><strong>if</strong>(x&lt;width/3):</blockquote><blockquote>col = 1</blockquote><blockquote><strong>elif</strong> (x&lt;width/3*2):</blockquote><blockquote>col = 2</blockquote><blockquote><strong>elif</strong>(x&lt;width):</blockquote><blockquote>col = 3</blockquote><blockquote>else:</blockquote><blockquote>col = None</blockquote><blockquote>#get row of mouse click (1–3)</blockquote><blockquote><strong>if</strong>(y&lt;height/3):</blockquote><blockquote>row = 1</blockquote><blockquote><strong>elif</strong> (y&lt;height/3*2):</blockquote><blockquote>row = 2</blockquote><blockquote><strong>elif</strong>(y&lt;height):</blockquote><blockquote>row = 3</blockquote><blockquote>else:</blockquote><blockquote>row = None</blockquote><blockquote>#print(row,col)</blockquote><blockquote><strong>if</strong>(row and col and TTT[row-1][col-1] is None):</blockquote><blockquote>global XO</blockquote><blockquote>#draw the x or o on screen</blockquote><blockquote><strong>drawXO</strong>(row,col)</blockquote><blockquote><strong>check_win</strong>()</blockquote><p>The last function is the reset_game(). This will restart the game and we also reset all the variables to the beginning of the game.</p><blockquote>def <strong>reset_game</strong>():</blockquote><blockquote>global TTT, winner,XO, draw</blockquote><blockquote>time.<strong>sleep</strong>(3)</blockquote><blockquote>XO = ‘x’</blockquote><blockquote>draw = False</blockquote><blockquote><strong>game_opening</strong>()</blockquote><blockquote>winner=None</blockquote><blockquote>TTT = [[None]*3,[None]*3,[None]*3]</blockquote><h3>5. Run the game forever</h3><p>To start the game, we will call the game_opening() function. Then, we run an infinite loop and continuously check for any event made by the user. If the user presses mouse button, the MOUSEBUTTONDOWN event will be captured and then we will trigger the userClick() function. Then if the user wins or the game draws, we reset the game by calling reset_game() function. We update the display in each iteration and we have set the frames per second to 30.</p><blockquote><strong>game_opening</strong>()</blockquote><blockquote># run the game loop forever</blockquote><blockquote><strong>while</strong>(True):</blockquote><blockquote>for event in pg.event.<strong>get</strong>():</blockquote><blockquote>if event.type == QUIT:</blockquote><blockquote>pg.<strong>quit</strong>()</blockquote><blockquote>sys.<strong>exit</strong>()</blockquote><blockquote>elif event.type is MOUSEBUTTONDOWN:</blockquote><blockquote># the user clicked; place an X or O</blockquote><blockquote><strong>userClick</strong>()</blockquote><blockquote><strong>if</strong>(winner or draw):</blockquote><blockquote><strong>reset_game</strong>()</blockquote><blockquote>pg.display.<strong>update</strong>()</blockquote><blockquote>CLOCK.<strong>tick</strong>(fps)</blockquote><p>The game is complete and ready to play.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be0e1ad55c66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/tic-tac-toe-python-game-project-memory-of-school-time-be0e1ad55c66">Tic Tac Toe Python Game Project — “Memory of school time”</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Auto-capture Selfie by Detecting Smile]]></title>
            <link>https://medium.com/analytics-vidhya/auto-capture-selfie-by-detecting-smile-1633218101ba?source=rss-1f237cb9f568------2</link>
            <guid isPermaLink="false">https://medium.com/p/1633218101ba</guid>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[selfie]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Akash Bhandari]]></dc:creator>
            <pubDate>Tue, 08 Sep 2020 08:15:00 GMT</pubDate>
            <atom:updated>2020-09-16T04:34:01.231Z</atom:updated>
            <content:encoded><![CDATA[<h3>Get beautiful selfies automatically captured when you smile — Python Project to automatically detect and capture selfies.</h3><p>Everyone loves a smiling picture, so we will develop a project which will capture images every time you smile. This is a simple machine learning project for beginners and we will use the OpenCV library.</p><h4>What is OpenCV?</h4><p><strong>OpenCV</strong> (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. <strong>OpenCV</strong> was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial products.</p><h3>Project Prerequisites</h3><p>To implement this project we need to know the following :</p><p>1. Basic concepts of Python<br>2. openCV basics.</p><p>To install the library, you can use pip installer from the command line:</p><ol><li>pip install OpenCV-python</li></ol><h3>Download training Code &amp; XML Files</h3><h3>Steps to Develop the Project</h3><p>Steps Involved to implement Smile Detection and Selfie Capture Project</p><ol><li>We first import the openCV library.</li><li>Now start webcam in the second line using the VideoCapture function of cv2.</li><li>Then, include haarcascade files in the python file.</li><li>Video is nothing but a series of images so we will run an infinite while loop for the same.</li><li>Then we are reading images from the video through read().</li><li>As feature recognition is more accurate in gray images we will convert the image to gray image using cvtColor() and BGR2GRAY which are basic openCV functions.</li><li>Now we will read faces using an already included haarcascade file and detectMultiscale() function where we pass gray image, ScaleFactor, and minNeighbors.</li></ol><ul><li>ScaleFactor: Parameter specifying zoom image, accuracy depends on it so we will keep it close to 1 but not very close as if we take 1.001(very close to 1), then it would detect even shadows so 1.1 is good enough for the face.</li><li>minNeighbors: Parameter specifying how many neighbors each rectangle should have to retain it.</li></ul><ol><li>If it detects a face we will draw an outer boundary of the face using rectangle() method of cv2 containing 5 arguments: image, initial point (x, y), an endpoint of principal diagonal (x + width, y + height), color of the rectangular periphery and last parameter is the thickness of drawn rectangular periphery.</li><li>If the face is detected then we will similarly detect a smile and if a smile is detected too we will print Image&lt;cnt&gt; saved in the cmd/terminal and then we have to provide the location of the folder in which we want to save the images.</li><li>To save the images we will use imwrite() which takes 2 parameters- location and image.</li><li>To prevent memory overflow we will just save 2 images in one run and thus useif statement which breaks the loop if cnt&gt;=2.</li><li>To break infinite loop, we have used an if statement which becomes true when we press ‘q’ denoting ‘quit’.</li><li>At last, we will release the video.</li><li>Do not forget to destroy all the windows.</li></ol><h3><strong>Code:</strong></h3><blockquote>import cv2<br>video = cv2.VideoCapture(0)</blockquote><blockquote>faceCascade=cv2.CascadeClassifier(“G:/dataset/haarcascade_frontalface_default.xml”)<br>smileCascade = cv2.CascadeClassifier(“G:/dataset/haarcascade_smile.xml”)</blockquote><blockquote>while True:<br> success,img = video.read()<br> grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<br> faces = faceCascade.detectMultiScale(grayImg,1.1,4)<br> cnt=1<br> keyPressed = cv2.waitKey(1)<br> for x,y,w,h in faces:<br> img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,0),0)<br> smiles = smileCascade.detectMultiScale(grayImg,1.8,15)<br> for x,y,w,h in smiles:<br> img = cv2.rectangle(img,(x,y),(x+w,y+h),(100,100,100),0)<br> print(“Image “+str(cnt)+”Saved”)<br> path=r’G:\dataset\img’+str(cnt)+’.jpg’<br> cv2.imwrite(path,img)<br> cnt +=1<br> if(cnt&gt;=2): <br> break<br> <br> cv2.imshow(‘live video’,img)<br> if(keyPressed &amp; 0xFF==ord(‘q’)):<br> break<br>video.release() <br>cv2.destroyAllWindows()</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1633218101ba" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/auto-capture-selfie-by-detecting-smile-1633218101ba">Auto-capture Selfie by Detecting Smile</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>