GMU:Bots 'n' Plots/Shubhra Bhatt: Difference between revisions

From Medien Wiki
No edit summary
No edit summary
Line 4: Line 4:


==== News Graffiti Twitter Bot ====
==== News Graffiti Twitter Bot ====
The News Graffiti Twitter Bot places speech bubbles on the images taken from the news accounts on twitter.


[[Image:Screen_Shot_2015-10-19_at_12.30.52_am.png]]
[[Image:Screen_Shot_2015-10-19_at_12.30.52_am.png]]


====  Screen Shot ====


The News Graffiti Twitter Bot places speech bubbles on the images taken from the news accounts on twitter.
[[Image:Screen_Shot_2015-10-19_at_12.32.00_am.png]]


[[Image:Screen_Shot_2015-10-19_at_12.32.00_am.png]]




== code ==
== code ==


import cv2
[http://pastebin.com/JPw0gKAf link to the code] <br>
import numpy as np
import random
from twitterbot import TwitterBot
import PIL
from PIL import Image, ImageDraw
from sys import argv
import os
 
debug = True
classifier = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(classifier)
 
 
def face_detect(image):
"""
Return rectangles of identified face regions
"""
 
# numpy grayscale image for face detection
array = np.asarray(image)
gray_image = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY)
 
# tweak this for better results ..
 
faces = faceCascade.detectMultiScale(
    gray_image,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(25, 25),
    flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
 
# convert boxes from from arrays to tuples
boxes = [(x, y, x + w, y + h) for (x, y, w, h) in faces]
return boxes
 
def face_mark(image):
"""
Mark faces with boxes
"""
 
# work on a copy
image = image.copy()
# identify boxes
boxes = face_detect(image)
 
# get drawing context for the current image
ctx = ImageDraw.Draw(image)
 
# define colors
black = (0, 0, 0, 255)
white = (255,255,255,255)
 
# draw boxes
for box in boxes:
 
# draw a black box
ctx.rectangle(box, fill=None, outline=black)
 
# draw a white box around it
x1, y1, x2, y2 = box
box = x1 - 1, y1 - 1, x2 + 1, y2 + 1
ctx.rectangle(box, fill=None, outline=white)
 
return image, boxes
 
def comic_element(image):
"""
Pasting Speech Bubbles on the Image
"""
 
#working on a copy of the image
image = image.copy()
 
# face detection
boxes=face_detect(image)
 
ctx = ImageDraw.Draw(image)
 
# grabing random files of img from the folder of speech bubbles
directory = os.path.join(os.path.dirname(__file__), "bubbles")
directory_list = os.listdir('./bubbles/')
 
# For MAC user - this is important step as iOS adds a hidden file called .DS
directory_list.remove('.DS_Store')
 
#finding the location for speech bubble and pasting it
for box in boxes:
 
 
#coordinate for face box
 
x1, y1, x2, y2 = box
 
# taking a random choice from the list created before
        one_bubble = random.choice(directory_list)
        # adding the name of the file on the rute so it can be opend later...
        imgfile = os.path.join(directory, one_bubble)
 
        # open the one_bubble img on the boxes
        sBubble= Image.open(imgfile)
 
        #resize the img of the bubble
        sBubble = sBubble.resize((160,160), resample=Image.ANTIALIAS)
        xS, yS = sBubble.size
        # Debugging  purpose
        #sBubble.show()
 
 
        # Finding the location for pasting the speech bubble
       
        # Centre Point coordinates
 
        cx = int((x2-x1)*0.5)
        # This value is passed to define the box for pasting image and it only takes integer value.
        cy = int((y2-y1)*0.5) 
 
        # Selecting either right or left side of the face for the bubble placement
 
        x_pos = random.choice((x1,x2))
 
        if x_pos == x1:
        x_pos = x_pos - xS
        y_pos =yS*(-1)
        else:
        y_pos =yS
 
        # Bubble box defines the size and place for pasting speech bubble on face
        bubblebox = (x_pos,cy,x_pos+xS,cy+yS)
 
 
        #left, upper coordinate
       
        px1 = int(r*np.math.cos(angle))
     
        py1 = int(r*np.math.sin(angle)+yS)
     
        image.paste(sBubble, bubblebox, sBubble)
        #image.show()
return image, boxes
 
 
 
 
if __name__ == '__main__':
 
"""
Loading Image
"""
src = Image.open("face1.jpg")
# face detection
boxes=face_detect(src)
img,box = comic_element(src)
img.show()
img.save("test_img.jpg")
 
 
 
 
 
 
 
 





Revision as of 23:03, 18 October 2015

Shubhra bhatt

News Graffiti Twitter Bot

The News Graffiti Twitter Bot places speech bubbles on the images taken from the news accounts on twitter.

Screen Shot 2015-10-19 at 12.30.52 am.png

Screen Shot

Screen Shot 2015-10-19 at 12.32.00 am.png


code

link to the code


First Python Image

My Bot-Bot.png

Code

""" My Bot-Bot """

def setup():

   size(400, 400)
   background(250, 250, 250)
   noStroke()
   fill(250, 138, 120)
   global pX
   pX = width/2
   global pY
   pY = 80

def draw():

   background(250, 250, 250)
       # Head , Eye & Body
   triangle(pX, pY + 80, pX - 80, pY, pX + 80, pY)
   fill(250, 240, 250)
   ellipse(pX - 20, pY + 20, 10, 10)
   ellipse(pX + 20, pY + 20, 10, 10)
   rect(pX - 10, pY + 50, 20, 4)
   
   fill(250, 138, 120)
       # Left Arm
   pushMatrix()
   translate(pX - 50, pY + 80)
   rect(0, 0, 100, 150)
   triangle(0, 0, -50, 80, -60, 50)
   ellipse(50, 180, 80, 80)
   fill(250, 240, 250)
   ellipse(50, 180, 60, 60)
   
       # Right Arm
   popMatrix()
   fill(250, 138, 120)
   pushMatrix()
   translate(pX + 50, pY + 80)
   scale(-1, 1)
   triangle(0, 0, -50, 80, -60, 50)
   popMatrix()    
   saveFrame("My Bot-Bot.png")


First Python Animation

AnimatedBot.gif

Code

""" My Bot-Bot animation """

def setup():

   size(400, 400)
   background(250, 250, 250)
   stroke(255, 240, 250)
   fill(250, 138, 120)
   global pX
   pX = 80
   global pY
   pY = 80
   global prV
   prV=10 
   

def draw():

   bgR = random(50,200)
   bgG = random(50,150)
   bgB = random(240,250)
   background(bgR,bgG,bgB)
   global prV
   global pX
   if pX>0 and pX<width:
      pX=pX+10
   elif pX>width:
       pX=pX-100
   else:
       pX=80    
       # Head , Eye & Body
   triangle(pX, pY + 80, pX - 80, pY, pX + 80, pY)
   fill(250, 240, 250)
   ellipse(pX - 20, pY + 20, 10, 10)
   ellipse(pX + 20, pY + 20, 10, 10)
   rect(pX - 10, pY + 50, 20, 4)


   fill(250, 138, 120)
       # Wheel
   pushMatrix()
   translate(pX - 50, pY + 80)
   rect(0, 0, 100, 150)
   ellipse(50, 180, 80, 80)
   fill(bgR, bgG, bgB)
   ellipse(50, 180, 60, 60)
   fill(250, 138, 120)
       # Left Arm
   rotate(-PI/8 * sin(frameCount * 0.1))
   triangle(0, 0, -50, 80, -60, 50)
   
   
       # Right Arm
   popMatrix()
   fill(250, 138, 120)
   pushMatrix()
   translate(pX + 50, pY + 80)
   rotate(PI/8 * sin(frameCount * 0.1))
   scale(-1, 1)
   triangle(0, 0, -50, 80, -60, 50)
   popMatrix()
   delay(80)
   saveFrame("bot###.png")