<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "##Sobel frame raw.py\n",
    "##<NAME>\n",
    "#1/5/2015\n",
    "\n",
    "##NOTE: comments that starts with ##GN: are the addition/modification needed\n",
    "##      to port the script over CPython from MicroPython - <NAME>\n",
    "\n",
    "import math\n",
    "\n",
    "from time import sleep as delay ##GN: changed import n.1: 'as delay' so no need to rewrite code\n",
    "from pyxi import OVERLAY        ##GN: changed import n.2\n",
    "from pyxi.video import Frame  ##GN: added import - we need this object in linux because we need contiguous memory allocation\n",
    "from pyxi.video.hdmi import HDMI\n",
    "from pyxi.video.vga import VGA\n",
    "\n",
    "OVERLAY().download_bitstream(\"audiovideo.bit\") ##GN: changed line of code - bitstream download is now slightly different\n",
    "\n",
    "vga = VGA('out')#define vga as an output port\n",
    "vga.mode(3) # 1280x720@60Hz\n",
    "#hdmi = HDMI('in', vga.frame_buffer())#HDMI is in capture mode 'in', not sure what second argument does.\n",
    "hdmi=HDMI('in')\n",
    "delay(10) \n",
    "hdmi.start()  ##GN: added line of code - now need to explicitly start HDMI as well\n",
    "delay(10) #takes time for the laptop display to output to HDMI\n",
    "frame_raw=hdmi.frame_raw()\n",
    "vga.start()#need to explicetly start VGA monitor\n",
    "vga.frame_raw(2, frame_raw)\n",
    "vga.frame_index(2)\n",
    "\n",
    "width = hdmi.frame_width()\n",
    "height = hdmi.frame_height()\n",
    "print('HDMI is capturing a video source of resolution {}x{}'.format(width,height))\n",
    "\n",
    "def rd_pix(vert, horiz, frame): #read the pixel at vert, horiz in frame and return a list containing rgb\n",
    "    green=frame[(1920*vert*3) + (3*horiz)]\n",
    "    blue=frame[(1920*vert*3) + (3*horiz) + 1]\n",
    "    red=frame[(1920*vert*3) + (3*horiz) + 2]\n",
    "    rd_pix=[]\n",
    "    rd_pix.append(red)\n",
    "    rd_pix.append(green)\n",
    "    rd_pix.append(blue)\n",
    "    return rd_pix\n",
    "\n",
    "def wr_pix(vert, horiz, frame, rgb): #write pixel at vert, horiz with colors in list rgb[]\n",
    "    frame[(1920*vert*3) + (3*horiz) + 2]=rgb[0] #write red pixel\n",
    "    frame[(1920*vert*3) + (3*horiz)]=rgb[1] #write green pixel\n",
    "    frame[(1920*vert*3) + (3*horiz) + 1]=rgb[2] #write blue pixel\n",
    "    return frame\n",
    "\n",
    "\n",
    "\n",
    "gray=Frame(width,height).frame  ##GN: changed line of code | allocates a gontiguous buffer. \n",
    "sobel=Frame(width,height).frame ##GN: changed line of code | the 'frame' attribute is the raw bytearray underneath this obj\n",
    "\n",
    "for v in range(0,1024):\n",
    "    for h in range(0,1280):\n",
    "        gr = round((0.299*rd_pix(v,h,frame_raw)[0]) +   (0.587*rd_pix(v,h,frame_raw)[1]) + (0.114*rd_pix(v,h,frame_raw)[2]))\n",
    "        rgb=[]\n",
    "        rgb.append(gr)\n",
    "        rgb.append(gr)\n",
    "        rgb.append(gr)\n",
    "        gray = wr_pix(v,h,gray,rgb)\n",
    "\n",
    "vga.frame_raw(0, gray)\n",
    "vga.frame_index(0)\n",
    "              \n",
    "#Compute the Sobel Filter output\n",
    "#Gx = -1 0 +1     Gy= +1 +2 +1\n",
    "#     -2 0 +2          0  0  0\n",
    "#     -1 0 +1         -1 -2 -1\n",
    "\n",
    "for q in range(1,1023):\n",
    "    for r in range(1,1279):\n",
    "        gx= abs(-rd_pix(q+1,r-1,gray)[0] + rd_pix(q+1,r+1,gray)[0] -2*rd_pix(q,r-1,gray)[0] +2*rd_pix(q,r+1,gray)[0] \\\n",
    "        - rd_pix(q-1,r-1,gray)[0] + rd_pix(q-1,r+1, gray)[0])\n",
    "        gy= abs(rd_pix(q+1,r-1, gray)[0] + 2*rd_pix(q+1,r,gray)[0] + rd_pix(q+1,r+1,gray)[0]  \\\n",
    "        - rd_pix(q-1,r-1,gray)[0]  -2*rd_pix(q-1,r,gray)[0] - rd_pix(q-1,r+1,gray)[0])\n",
    "        grad=gx + gy\n",
    "        if grad>255:\n",
    "            grad=255\n",
    "        res=[]\n",
    "        res.append(grad)\n",
    "        res.append(grad)\n",
    "        res.append(grad)\n",
    "        sobel = wr_pix(q,r,sobel,res)\n",
    "\n",
    "vga.frame_raw(1, sobel)\n",
    "vga.frame_index(1)\n",
    "\n",
    "delay(10) #GN: let user see result before exiting the script\n",
    "del gray #GN: need to release memory\n",
    "del sobel #GN: need to release memory\n",
    "del vga #GN: need to release memory\n",
    "del hdmi #GN: need to release memory changed import n.1: 'as delay' so no need to rewrite code\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"<commit_msg>Update Sobel_filter_by_Liam_Madden.ipynb<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "##Sobel frame raw.py\n",
    "##<NAME>\n",
    "#1/5/2015\n",
    "\n",
    "##NOTE: comments that starts with ##GN: are the addition/modification needed\n",
    "##      to port the script over CPython from MicroPython - <NAME>\n",
    "\n",
    "import math\n",
    "\n",
    "from time import sleep as delay ##GN: changed import n.1: 'as delay' so no need to rewrite code\n",
    "from pyxi import OVERLAY        ##GN: changed import n.2\n",
    "from pyxi.video import Frame  ##GN: added import - we need this object in linux because we need contiguous memory allocation\n",
    "from pyxi.video.hdmi import HDMI\n",
    "from pyxi.video.vga import VGA\n",
    "\n",
    "OVERLAY().download_bitstream(\"audiovideo.bit\") ##GN: changed line of code - bitstream download is now slightly different\n",
    "\n",
    "vga = VGA('out')#define vga as an output port\n",
    "vga.mode(3) # 1280x720@60Hz\n",
    "#hdmi = HDMI('in', vga.frame_buffer())#HDMI is in capture mode 'in', not sure what second argument does.\n",
    "hdmi=HDMI('in')\n",
    "delay(10) \n",
    "hdmi.start()  ##GN: added line of code - now need to explicitly start HDMI as well\n",
    "delay(10) #takes time for the laptop display to output to HDMI\n",
    "frame_raw=hdmi.frame_raw()\n",
    "vga.start()#need to explicetly start VGA monitor\n",
    "vga.frame_raw(2, frame_raw)\n",
    "vga.frame_index(2)\n",
    "\n",
    "width = hdmi.frame_width()\n",
    "height = hdmi.frame_height()\n",
    "print('HDMI is capturing a video source of resolution {}x{}'.format(width,height))\n",
    "\n",
    "def rd_pix(vert, horiz, frame): #read the pixel at vert, horiz in frame and return a list containing rgb\n",
    "    green=frame[(1920*vert*3) + (3*horiz)]\n",
    "    blue=frame[(1920*vert*3) + (3*horiz) + 1]\n",
    "    red=frame[(1920*vert*3) + (3*horiz) + 2]\n",
    "    rd_pix=[]\n",
    "    rd_pix.append(red)\n",
    "    rd_pix.append(green)\n",
    "    rd_pix.append(blue)\n",
    "    return rd_pix\n",
    "\n",
    "def wr_pix(vert, horiz, frame, rgb): #write pixel at vert, horiz with colors in list rgb[]\n",
    "    frame[(1920*vert*3) + (3*horiz) + 2]=rgb[0] #write red pixel\n",
    "    frame[(1920*vert*3) + (3*horiz)]=rgb[1] #write green pixel\n",
    "    frame[(1920*vert*3) + (3*horiz) + 1]=rgb[2] #write blue pixel\n",
    "    return frame\n",
    "\n",
    "\n",
    "\n",
    "gray=Frame(width,height).frame  ##GN: changed line of code | allocates a gontiguous buffer. \n",
    "sobel=Frame(width,height).frame ##GN: changed line of code | the 'frame' attribute is the raw bytearray underneath this obj\n",
    "\n",
    "for v in range(0,1024):\n",
    "    for h in range(0,1280):\n",
    "        gr = round((0.299*rd_pix(v,h,frame_raw)[0]) +   (0.587*rd_pix(v,h,frame_raw)[1]) + (0.114*rd_pix(v,h,frame_raw)[2]))\n",
    "        rgb=[]\n",
    "        rgb.append(gr)\n",
    "        rgb.append(gr)\n",
    "        rgb.append(gr)\n",
    "        gray = wr_pix(v,h,gray,rgb)\n",
    "\n",
    "vga.frame_raw(0, gray)\n",
    "vga.frame_index(0)\n",
    "              \n",
    "#Compute the Sobel Filter output\n",
    "#Gx = -1 0 +1     Gy= +1 +2 +1\n",
    "#     -2 0 +2          0  0  0\n",
    "#     -1 0 +1         -1 -2 -1\n",
    "\n",
    "for q in range(1,1023):\n",
    "    for r in range(1,1279):\n",
    "        gx= abs(-rd_pix(q+1,r-1,gray)[0] + rd_pix(q+1,r+1,gray)[0] -2*rd_pix(q,r-1,gray)[0] +2*rd_pix(q,r+1,gray)[0] \\\n",
    "        - rd_pix(q-1,r-1,gray)[0] + rd_pix(q-1,r+1, gray)[0])\n",
    "        gy= abs(rd_pix(q+1,r-1, gray)[0] + 2*rd_pix(q+1,r,gray)[0] + rd_pix(q+1,r+1,gray)[0]  \\\n",
    "        - rd_pix(q-1,r-1,gray)[0]  -2*rd_pix(q-1,r,gray)[0] - rd_pix(q-1,r+1,gray)[0])\n",
    "        grad=gx + gy\n",
    "        if grad>255:\n",
    "            grad=255\n",
    "        res=[]\n",
    "        res.append(grad)\n",
    "        res.append(grad)\n",
    "        res.append(grad)\n",
    "        sobel = wr_pix(q,r,sobel,res)\n",
    "\n",
    "vga.frame_raw(1, sobel)\n",
    "vga.frame_index(1)\n",
    "\n",
    "delay(10) #GN: let user see result before exiting the script\n",
    "del gray #GN: need to release memory\n",
    "del sobel #GN: need to release memory\n",
    "del vga #GN: need to release memory\n",
    "del hdmi #GN: need to release memory\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
{'fraction_non_alphanumeric': 0.20530084428769904, 'fraction_numerical': 0.03644330447793096, 'mean_word_length': 2.5167230364524613, 'pattern_counts': {'":': 24, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7144405', 'n_tokens_mistral': 3976, 'n_tokens_neox': 3787, 'n_words': 849}