[974c13]: / AnalysisCodes / SSD / dividingImages.ipynb

Download this file

118 lines (117 with data), 3.6 kB

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'cv2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-52622fdc5d50>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcsv\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mshutil\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mPIL\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mImage\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'cv2'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import csv\n",
    "import shutil\n",
    "import cv2\n",
    "import numpy \n",
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.patches as patches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('trains.txt') as filenames:\n",
    "    reader = csv.reader(filenames)\n",
    "    for f in reader:\n",
    "        shutil.move(\"./images/\" + f[0][:-4] + '.jpg' , \"./trainImages\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('testimages.txt') as filenames:\n",
    "    reader = csv.reader(filenames)\n",
    "    for f in reader:\n",
    "        shutil.move(\"./images/\" + f[0][:-4] + '.jpg', \"./testImages\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "allfiles=os.listdir('images')\n",
    "imlist=[filename for filename in allfiles if  filename[-4:] in [\".jpg\",\".JPG\"]]\n",
    "\n",
    "\n",
    "w,h=Image.open('images/' + imlist[0]).size\n",
    "N=len(imlist)\n",
    "\n",
    "res = 0\n",
    "\n",
    "for im in imlist:\n",
    "    tempim = Image.open('images/' + im)\n",
    "    w, h = tempim.size\n",
    "    \n",
    "    imarr=numpy.array(tempim,dtype=numpy.float)\n",
    "    if(len(imarr.shape) == 3):\n",
    "        imarr = imarr[:,:,0]\n",
    "    avg = 0\n",
    "    for x in range(w):\n",
    "        for y in range(h):\n",
    "            avg = avg + imarr[y][x]\n",
    "    avg = avg/(w*h)\n",
    "    res = res + avg/N\n",
    "\n",
    "print(res)\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}